创建spring mvc 框架


1:右键项目-Dynamic Web Project 

2:web.xml文件 WEB_INF 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<!-- 
配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求 
-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置 DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
<!-- 
实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
-->
<!--  
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
dispatcher-servlet.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 配置自定扫描的包 -->
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>
                HiddenHttpMethodFilter
在web.xml中添加
<!-- 
    配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POST 请求 
    -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
添加国际资源化文件
 添加jar文件 jstl.jar 和    standard.jar
 jstl 添加后 view变成jstlView 
在src下添加 il8n.properties  国际文件 
i18n.username=Username
i18n.password=Password
il8n_zh_CN.propertise     中文文件
i18n.username=\u7528\u6237\u540D
i18n.password=\u5BC6\u7801
il8n_en_US.propertise 英文文件 
i18n.username=Username
i18n.password=Password
<!-- 配置国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
jsp中设置fmt
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
username :
<fmt:message key="i18n.username"></fmt:message>    
<fmt:message key="i18n.password"></fmt:message> 
                                                                             直接转发页面
servlet 中配置 项目地址+"/success"
<!-- 配置直接转发的页面 -->
<!-- 可以直接相应转发的页面, 而无需再经过 Handler 的方法.  -->
<mvc:view-controller path="/success" view-name="success"/>
<!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->
<mvc:annotation-driven></mvc:annotation-driven>

 

自定义视图解析器 
<!-- 配置视图  BeanNameViewResolver 解析器: 使用视图的名字来解析视图 -->
<!-- 通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>  
</bean>
java 视图类
@Component
public class HelloView implements View {
    @Override
    public String getContentType() {
        return "text/html";
    }
    @Override
    public void render(Map<String, ?> arg0, HttpServletRequest arg1,
            HttpServletResponse arg2) throws Exception {
        arg2.getWriter().print("hello  view :"+ new Date());
        
    }
controller 控制器 
@RequestMapping("/hello")
        public String  helloWorld(){
        System.out.println("helloView");
            return  " helloView";
        }  
重定向 

 

"redirect:/index.jsp"    jsp 为根目录下 WebContent下     只能是这种方式 
网站地址为 
forward:地址不变   值会传过去  地址为请求的地址  
"forward:/WEB-INF/views/success.jsp"  放在views文件下 
"forward:/success.jsp"  但是要放在根目录下
处理静态资源
B-INF/views/success.jsp"  放在views文件下 

添加这两个标签 

<mvc:default-servlet-handler/>

<mvc:annotation-driven></mvc:annotation-driven>
















posted @ 2016-03-10 15:05  众人皆醉,唯我独醒  阅读(152)  评论(0)    收藏  举报