Spring的配置

来源:互联网 发布:怎么申请淘宝介入退款 编辑:程序博客网 时间:2024/06/07 03:04
  1. 一、web.xml配置Spirng

  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.     version="2.5">  
  7.   
  8.     <display-name>Archetype Created Web Application</display-name>  
  9.   
  10.     <!--   
  11.         1:指明配置文件位置,默认位置为WEB-INF,默认文件名为applicationContext.xml  
  12.           
  13.         Q.  web.xml中classpath:和classpath*:  有什么区别?  
  14.         A.  classpath:只会到你的class路径中查找找文件;     
  15.             classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.  
  16.           
  17.         Q.  位于不同目录下的配置文件,该如何定义?  
  18.         A.  src的情况,需要在web.xml中定义如下:    
  19.             <context-param>    
  20.                 <param-name>contextConfigLocation</param-name>    
  21.                 <param-value>classpath:applicationContext.xml</param-value>    
  22.             < /context-param>  
  23.               
  24.             WEB-INF的情况,需要在web.xml中定义如下:    
  25.             <context-param>    
  26.                 <param-name>contextConfigLocation</param-name>    
  27.                 <param-value>WEB-INF/applicationContext*.xml</param-value>    
  28.             < /context-param>  
  29.           
  30.         Q.  如何添加多个配置文件?  
  31.         A.  <context-param>     
  32.                 <param-name>contextConfigLocation</param-name>     
  33.                 <param-value>     
  34.                     classpath*:conf/applicationContext_core*.xml,     
  35.                     classpath*:conf/applicationContext_bean*.xml,     
  36.                     classpath*:conf/applicationContext_jdbc*.xml,     
  37.                     classpath*:conf/applicationContext_log*.xml     
  38.                 </param-value>     
  39.             </context-param>  
  40.     -->  
  41.     <context-param>  
  42.         <param-name>contextConfigLocation</param-name>  
  43.         <param-value>  
  44.             classpath*:applicationContext*.xml  
  45.         </param-value>  
  46.     </context-param>  
  47.   
  48.     <!--   
  49.         2:初始化配置监听器  
  50.           
  51.         ContextLoaderListener的作用在于,容器启动时自动装配applicationContext.xml(默认情况,可设置,参上)的配置信息  
  52.      -->  
  53.     <listener>  
  54.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  55.     </listener>  
  56.   
  57.     <!--   
  58.         3:字符编码拦截器,解决中文乱码问题  
  59.           
  60.         中文编码乱码问题,是所有中国程序员最常见的问题之一。在使用SpringMVC时,尤其做ajax请求,很容易出现乱码问题。  
  61.         很多时候我们会很奇怪,明明在tomcat的server.xml中配置了<Connector URIEncoding="UTF-8" ...>,结果还是出现了乱码。  
  62.         其实,这里的配置只是对url进行了编码处理,只要是get请求,参数都是通过url传递,是不会有乱码问题的。但post请求时,  
  63.         因为参数都在请求体里,这里的编码设置并不能影响请求体编码,所以就容易出现乱码。  
  64.           
  65.         如果是firefox浏览器,post请求会自动带上请求头信息:content-type = application/x-www-form-urlencoded; charset=UTF-8  
  66.         所以firefox下SpringMVC可能会表现良好,没有乱码问题;  
  67.         但如果是chrome浏览器,它不会设置关于编码的请求头信息:content-type = application/x-www-form-urlencoded ,  
  68.         而SpringMVC默认又是采用的ISO-8859-1解析参数,就会出现乱码  
  69.           
  70.         解决方案:  
  71.         a.  配置请求映射时:@RequestMapping(value = "saveUserByJson"produces = { "text/json;charset=UTF-8" })  
  72.         b.  全局过滤,即以下配置,无需自己写过滤器,spring已经提供CharacterEncodingFilter  
  73.     -->  
  74.     <filter>  
  75.         <filter-name>forceEncoding</filter-name>  
  76.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  77.         <init-param>  
  78.             <param-name>forceEncoding</param-name>  
  79.             <param-value>true</param-value>  
  80.         </init-param>  
  81.         <init-param>  
  82.             <param-name>encoding</param-name>  
  83.             <param-value>utf-8</param-value>  
  84.         </init-param>  
  85.     </filter>  
  86.   
  87.     <filter-mapping>  
  88.         <filter-name>forceEncoding</filter-name>  
  89.         <url-pattern>/*</url-pattern>  
  90.     </filter-mapping>  
  91.   
  92.     <!--   
  93.         4:拦截处理所有请求  
  94.           
  95.         DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:  
  96.         a、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;  
  97.         b、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);  
  98.         c、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);  
  99.         d、通过ViewResolver解析逻辑视图名到具体视图实现;  
  100.         e、本地化解析;  
  101.         f、渲染具体的视图等;  
  102.         g、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。  
  103.           
  104.         可以看出,使用SpringMVC时,几乎大部分功能都是通过该servlet请求转发的。  
  105.           
  106.         DispatcherServlet默认是以WebApplicationContext作为上下文的,默认的配置文件为[servlet-name]-servlet.xml,  
  107.         比如这里的servlet-name是spring,所以默认的配置文件应该是spring-servlet.xml,且默认情况下,这个文件应放在WEB-INF目录下。  
  108.           
  109.         也可以通过初始化参数来设置上下文的配置文件,方式如下。  
  110.     -->  
  111.     <servlet>  
  112.         <servlet-name>spring</servlet-name>  
  113.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  114.         <init-param>  
  115.             <param-name>contextConfigLocation</param-name>  
  116.             <param-value>classpath:spring-servlet-config.xml</param-value>  
  117.         </init-param>  
  118.     </servlet>  
  119.   
  120.     <!--   
  121.         5:映射拦截请求路径  
  122.           
  123.         url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问  
  124.     -->  
  125.     <servlet-mapping>  
  126.         <servlet-name>spring</servlet-name>  
  127.         <url-pattern>/*</url-pattern>  
  128.     </servlet-mapping>  
  129.   
  130.     <!--   
  131.         6:指定系统欢迎页  
  132.           
  133.         该设置指定,当用户访问到目录时,系统依次尝试访问以下页面,直到发现可用的页面  
  134.      -->  
  135.     <welcome-file-list>  
  136.         <welcome-file>index.jsp</welcome-file>  
  137.         <welcome-file>index.html</welcome-file>  
  138.         <welcome-file>default.jsp</welcome-file>  
  139.         <welcome-file>default.html</welcome-file>  
  140.     </welcome-file-list>  
  141.       
  142.     <!--   
  143.         7:错误页面  
  144.           
  145.         error-page配置用于在用户请求资源时发生错误的情况下,提供默认的页面以提示用户。比如用户请求了不存在的资源,  
  146.         如果不做设置,用户就会看到简单粗暴的404错误页面;再比如系统实现中存在漏洞,在各种条件下触发空指针异常,用户就会  
  147.         看到一大段的异常堆栈。这些都是不友好的用户体验,而error-page则帮我们实现了在指定错误下跳转到指定页面的功能。  
  148.           
  149.         error-code指出在给定http错误码返回时,要展示给用户的页面;而exception-type则表示在系统出现某种异常时,要展示给  
  150.         用户的页面。  
  151.      -->  
  152.     <error-page>    
  153.         <error-code>404</error-code>    
  154.         <location>/WEB-INF/common/404.html</location>    
  155.     </error-page>  
  156.       
  157.     <error-page>    
  158.         <error-code>500</error-code>    
  159.         <location>/WEB-INF/common/500.html</location>    
  160.     </error-page>  
  161.       
  162.     <error-page>  
  163.         <exception-type>java.lang.NullPointerException</exception-type>    
  164.         <location>/WEB-INF/common/nullpointer.html</location>    
  165.      </error-page>  
  166.        
  167.         
  168.     <!--   
  169.         8:设置session过期时长  
  170.           
  171.         当session查出session-timeout指定的时长时,整个session就会过期。  
  172.         session-timeout的单位是分钟,所以下面的配置session会在用户3小时无操作时过期  
  173.     -->  
  174.     <session-config>  
  175.         <session-timeout>180</session-timeout>  
  176.     </session-config>  
  177.   
  178. </web-app>  
0 0
原创粉丝点击