SpringMVC配置

使用SpringMVC开发框架,最重要也是最核心的部分就是各种配置文件。配置文件稍有错误,极有可能造成整个项目启动失败或无法访问。而SpringMVC又是包含着一个大的配置文件生态系统,比如使用日志时的日志配置、使用freemarker时的模板配置、使用数据源时的jdbc配置等等。

本文记录一些重要的配置文件,并逐一完善各个配置文件,使项目能够处理更加复杂的业务。

一、web.xml

web.xml存放在WEB-INF.xml目录下,这里有关于web.xml的作用

<?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-servlet.xml

spring-servlet.xml并不一定是spring-servlet.xml,它的文件名对应于DispatcherServlet配置的servlet-name属性,其中spring就是servlet-name的取值。如果没有对该servlet进行初始化参数的配置,这个配置文件应该存放在WEB-INF目录下。

在SpringMVC中,applicationContext是mvc context的父容器,mvc context可以引用到applicationContext中的bean,反之则不行。mvc context主要用于配置控制器、拦截器以及适配器相关的设置,它是针对web层的;而applicationContext则用于对整个应用进行配置,比如dao层、service层等等,它不关注表现层。

spring在查找bean时,如果能在当前上下文中找到,则返回,否则就会到父容器中查找。








发布了40 篇原创文章 · 获赞 6 · 访问量 10万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章