1.头、尾信息
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
</beans>
2.配置spring组件扫描
扫描cn.csdn.ssm.controller包下的注解,来对bean进行统一的管理
- @Service——用于注释业务层组件
- @Repository——用于注释数据的访问层
- @Controller——用于注释控制层
- @Component——当bean对象的层次不是很清晰时,用该注解来注释
<context:component-scan
base-package="cn.csdn.ssm.controller" />
3.静态资源解析
这里表示当访问地址包括js或img时,就加载js和img文件夹的静态资源
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/img/" mapping="/img/**" />
4.annotation-driven
通过annotation-driven可以替代下边的处理器映射器和适配器
<mvc:annotation-driven
conversion-service="conversionService">
</mvc:annotation-driven>
5.注解处理器映射器
该类为springmvc的内部对象,不需要id
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>
6.注解适配器
注解适配器和上面的注解处理器映射器匹配使用, 在webBindingInitializer中注入自定义属性编辑器、自定义转换器,加入 json数据的消息转换器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.
MappingJacksonHttpMessageConverter">
</bean>
</list>
</property>
</bean>
7.视图解析器
可以在视图解析器中设置访问路径的前缀和后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
8.自定义webBinder
在webBinder中可以配置converter进行参数转换,配置validator检验后台信息,属性编辑器
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
<property name="propertyEditorRegistrars">
<list>
<ref bean="customPropertyEditor"
</list>
</property>
</bean>
9.转换器配置
可以将自己的转换器注册
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="cn.csdn.ssm.controller.converter.CustomDateConverter" />
<bean class="cn.csdn.ssm.controller.converter.StringTrimConverter" />
</list>
</property>
</bean>
10.检验器
指定校验使用的资源文件,如果不指定则默认使用classpath下的ValidationMessages.properties
<bean id="validator" class="org.springframework.
validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource" />
</bean>
11.校验错误信息配置文件
可以设置文件的格式和缓存时间
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list> <value>classpath:CustomValidationMessages</value>
</list>
</property>
<property name="fileEncodings" value="utf-8" />
<property name="cacheSeconds" value="120" />
</bean>
12.属性编辑器
该类为自己创建的,需实现PropertyEditorRegistrar接口
<bean id="customPropertyEditor"
class="cn.csdn.ssm.controller.propertyeditor.CustomPropertyEditor">
</bean>
13.全局文件上传
可以限制上传文件的大小
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
14.定义统一异常处理器
自己创建的类,需要实现HandlerExceptionResolver接口
<bean class="cn.csdn.ssm.exception.CustomExceptionResolver"></bean>
15.拦截器
执行自己定义的拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cn.csdn.ssm.controller.interceptor.HandlerInterceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cn.csdn.ssm.controller.interceptor.HandlerInterceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>