3.web.xml配置文件

来源:互联网 发布:三剑客软件下载 编辑:程序博客网 时间:2024/06/16 10:07

注:如果web.xml没有提示信息,参考http://blog.csdn.net/qq_18570273/article/details/51009137

1.启动Spring容器,并配置Spring的地址

<!-- needed for ContextLoaderListener -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>    <!-- Bootstraps the root web application context before servlet initialization -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>

2.spring的前端控制器,拦截所有请求

注:可以不指定springmvc的位置,但是一定要在web.xml的同级目录下(也就是WEB-INF文件夹下),而且名字必须是 servlet-name加上“-servlet.xml”(我的名字是:dispathcherServlet-servlet.xml)

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->    <servlet>        <servlet-name>dispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Map all requests to the DispatcherServlet for handling -->    <servlet-mapping>        <servlet-name>dispatcherServlet</servlet-name>        <url-pattern>/*</url-pattern>    </servlet-mapping>

3.配置字符编码过滤器

<!-- 3.配置字符编码过滤器 -->    <filter>        <filter-name>CharacterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>utf-8</param-value>        </init-param>        <init-param>            <param-name>forceResponseEncoding</param-name>            <param-value>true</param-value>        </init-param>        <init-param>            <param-name>forceRequestEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

4.使用Rest风格的URI,将页面普通的post请求转化为指定的GET请求或者PUT请求

<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>
原创粉丝点击