- 一、web.xml配置Spirng
-
- <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <display-name>Archetype Created Web Application</display-name>
- <!--
- 1:指明配置文件位置,默认位置为WEB-INF,默认文件名为applicationContext.xml
- Q. web.xml中classpath:和classpath*: 有什么区别?
- A. classpath:只会到你的class路径中查找找文件;
- classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
- Q. 位于不同目录下的配置文件,该如何定义?
- A. src的情况,需要在web.xml中定义如下:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- < /context-param>
- WEB-INF的情况,需要在web.xml中定义如下:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/applicationContext*.xml</param-value>
- < /context-param>
- Q. 如何添加多个配置文件?
- A. <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath*:conf/applicationContext_core*.xml,
- classpath*:conf/applicationContext_bean*.xml,
- classpath*:conf/applicationContext_jdbc*.xml,
- classpath*:conf/applicationContext_log*.xml
- </param-value>
- </context-param>
- -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath*:applicationContext*.xml
- </param-value>
- </context-param>
- <!--
- 2:初始化配置监听器
- ContextLoaderListener的作用在于,容器启动时自动装配applicationContext.xml(默认情况,可设置,参上)的配置信息
- -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--
- 3:字符编码拦截器,解决中文乱码问题
- 中文编码乱码问题,是所有中国程序员最常见的问题之一。在使用SpringMVC时,尤其做ajax请求,很容易出现乱码问题。
- 很多时候我们会很奇怪,明明在tomcat的server.xml中配置了<Connector URIEncoding="UTF-8" ...>,结果还是出现了乱码。
- 其实,这里的配置只是对url进行了编码处理,只要是get请求,参数都是通过url传递,是不会有乱码问题的。但post请求时,
- 因为参数都在请求体里,这里的编码设置并不能影响请求体编码,所以就容易出现乱码。
- 如果是firefox浏览器,post请求会自动带上请求头信息:content-type = application/x-www-form-urlencoded; charset=UTF-8
- 所以firefox下SpringMVC可能会表现良好,没有乱码问题;
- 但如果是chrome浏览器,它不会设置关于编码的请求头信息:content-type = application/x-www-form-urlencoded ,
- 而SpringMVC默认又是采用的ISO-8859-1解析参数,就会出现乱码
- 解决方案:
- a. 配置请求映射时:@RequestMapping(value = "saveUserByJson", produces = { "text/json;charset=UTF-8" })
- b. 全局过滤,即以下配置,无需自己写过滤器,spring已经提供CharacterEncodingFilter
- -->
- <filter>
- <filter-name>forceEncoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>forceEncoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--
- 4:拦截处理所有请求
- DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
- a、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
- b、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
- c、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
- d、通过ViewResolver解析逻辑视图名到具体视图实现;
- e、本地化解析;
- f、渲染具体的视图等;
- g、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。
- 可以看出,使用SpringMVC时,几乎大部分功能都是通过该servlet请求转发的。
- DispatcherServlet默认是以WebApplicationContext作为上下文的,默认的配置文件为[servlet-name]-servlet.xml,
- 比如这里的servlet-name是spring,所以默认的配置文件应该是spring-servlet.xml,且默认情况下,这个文件应放在WEB-INF目录下。
- 也可以通过初始化参数来设置上下文的配置文件,方式如下。
- -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-servlet-config.xml</param-value>
- </init-param>
- </servlet>
- <!--
- 5:映射拦截请求路径
- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问
- -->
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- <!--
- 6:指定系统欢迎页
- 该设置指定,当用户访问到目录时,系统依次尝试访问以下页面,直到发现可用的页面
- -->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.html</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- </welcome-file-list>
- <!--
- 7:错误页面
- error-page配置用于在用户请求资源时发生错误的情况下,提供默认的页面以提示用户。比如用户请求了不存在的资源,
- 如果不做设置,用户就会看到简单粗暴的404错误页面;再比如系统实现中存在漏洞,在各种条件下触发空指针异常,用户就会
- 看到一大段的异常堆栈。这些都是不友好的用户体验,而error-page则帮我们实现了在指定错误下跳转到指定页面的功能。
- error-code指出在给定http错误码返回时,要展示给用户的页面;而exception-type则表示在系统出现某种异常时,要展示给
- 用户的页面。
- -->
- <error-page>
- <error-code>404</error-code>
- <location>/WEB-INF/common/404.html</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/WEB-INF/common/500.html</location>
- </error-page>
- <error-page>
- <exception-type>java.lang.NullPointerException</exception-type>
- <location>/WEB-INF/common/nullpointer.html</location>
- </error-page>
- <!--
- 8:设置session过期时长
- 当session查出session-timeout指定的时长时,整个session就会过期。
- session-timeout的单位是分钟,所以下面的配置session会在用户3小时无操作时过期
- -->
- <session-config>
- <session-timeout>180</session-timeout>
- </session-config>
- </web-app>
Spring的配置
最新推荐文章于 2024-07-24 14:18:45 发布