上一篇讲述了Spring mvc 中常用的几个注解标示,这章中主要讲述spring mvc 的一些配置和应该注意的地方,已经试图,模板的使用。
首先需要在Web.xml 中添加spring 的加载器,在spring mvc 中其实你不写ContextLoaderListener 这个 跑起来基本上是没什么问题的,但是如果你要对一些Bean 的加载进行监听的话,建议你还是老老实实的加上这个类。多余的就不说了,请看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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springContentConfig.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springMvcConfig.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app>
在此文件中我们定义了spring 配置的文件springContentConfig.xml,MVC Controller 的配置文件springMvcConfig.xml,如果没有指定此文件名称,spring就会按照servelt 的名称去找该配置文件。另外此xml中还配置了转码一个类以及log4j的配置文件
下来我们首先看一下 spring 的配置文件springContentConfig.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <import resource="classpath:/DBConfig.xml"/> <context:component-scan base-package="com.my.springmvc"> <context:exclude-filter type="regex" expression="com.my.springmvc.web.*"/> </context:component-scan> <bean class="com.my.springmvc.util.SpringBeanInitProcesser" /> <bean class="com.my.springmvc.util.SpringContentUtil" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="-1" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="defaultEncoding" value="UTF-8"></property> <property name="host" value="smtp.sina.cn"/> <property name="username" value="****"></property> <property name="password" value="***"></property> </bean> <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </value> </property> </bean> </beans>
此处(<import resource="classpath:/DBConfig.xml"/>)我们引用了DB的配置信息xml文件,这个后面再说。
<context:component-scan/>配置了扫描的注解的范围。
<context:exclude-filter/>配置扫描自动校验注解的范围
<bean class="com.my.springmvc.util.SpringBeanInitProcesser" />这个配置的是监视spring 加载bean的一个类,此类需要实现接口 org.springframework.beans.factory.config.BeanPostProcessor
<bean class="com.my.springmvc.util.SpringContentUtil" />该类是一个工具类,可以自由的拿取spring 中的bean,此类需要实现接口org.springframework.context.ApplicationContextAware
下面几个就不在详解了,是配置上传文件,模板,还有邮件的一些配置。
下来看看我这个里面DB是怎么配置的DBConfig.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:/jdbc.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>${jdbc.driver}</value></property> <property name="url"><value>${jdbc.url}</value></property> <property name="username"><value>${jdbc.username}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
很简单 呵呵这个里面就是对配置文件jdbc.properties内容的设置
spring 可以说对后台的东西都over了,下面看看springMvcConfig.xml里面如何配置mvc的东西
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.my.springmvc.web"/> <mvc:resources mapping="/static/**" location="/static/"/> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView"> <value>exception/exception</value> </property> <property name="exceptionMappings"> <props> <prop key="java.sql.SQLException">exception/sqlException</prop> <prop key="java.lang.RuntimeException">exception/runException</prop> </props> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="order" value="0" /> <property name="cacheSeconds" value="0" /> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" > <property name="validator" ref="validator"/> </bean> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > </bean> </list> </property> </bean> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>classpath:/com/my/springmvc/spring-excel-views.xml</value> </property> <property name="order" value="0"></property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/views/"/> <property name="suffix" value=".jsp"/> <property name="order" value="1"></property> </bean> </beans>
前面几个相同的我就不说了,<mvc:resources mapping="/static/**" location="/static/"/>这个是配置一些静态资源的,这样配置之后,请求这些东西的时候spring 就不用进行过滤了。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
这个是用来校验的。
<bean id="exceptionResolver" />这个是来配置出现异常时呈现给用户的页面
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>classpath:/com/my/springmvc/spring-excel-views.xml</value>
</property>
<property name="order" value="0"></property>
</bean>
这个是来配置试图的,我这有两个视图的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="excelSystemMenuView" class="com.my.springmvc.web.view.ExcelSystemMenuView"> </bean> <bean id="pdfSysmenuView" class="com.my.springmvc.web.view.PDFSystemMenuView"></bean> </beans>
一个是Excel 的,一个是PDF的
在mvc 那个文件中还有两个比较重要的配置
1。<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="order" value="0" />
<property name="cacheSeconds" value="0" />
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer" >
<property name="validator" ref="validator"/><--前面配置的validate就是在这里使用的-->
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" ><!--这一堆都是配置信息转换的-->
</bean>
</list>
</property>
</bean>
2。
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1"></property>
</bean>
这一段是视图解析,prefix 指定以项目目录为根的相对路径,suffix 指定 视图的文件的后缀。这个你可以配置多个,在代码中具体进行调用。