springmvc(1)--配置
最近把spring的使用整理下,版本4.1.1.RELEASE
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。
<!--spring-mvc入口--> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:servlet-context.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet>
servlet-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--处理静态资源--> <mvc:resources mapping="/resources/**" location="/resources/"/> <!--注解类所在jar包--> <context:component-scan base-package="springtry.web.controller"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!--启动注解处理(@Controller等)--> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" conversion-service="conversionService"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <!-- 处理请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!--json转换器--> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"><!--request/response的Content-Typ--> <list> <value>text/html;charset=UTF-8</value><!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <!-- <bean class="***" /> --> </set> </property> </bean> <!--视图协商管理器--> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager"> <constructor-arg> <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> <constructor-arg> <map> <entry key="json" value="application/json;charset=utf-8"/> <entry key="xml" value="application/xml;charset=utf-8"/> </map> </constructor-arg> </bean> </constructor-arg> </bean> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/vm/"/> <property name="velocityProperties"> <props> <prop key="directive.foreach.counter.name">loopCounter</prop> <prop key="directive.foreach.counter.initial.value">0</prop> <prop key="input.encoding">UTF-8</prop> <prop key="output.encoding">UTF-8</prop> <prop key="directive.foreach.counter.name">velocityCount</prop> <prop key="directive.foreach.counter.initial.value">1</prop> <prop key="velocimacro.library.autoreload">true</prop> </props> </property> </bean> <!--支持在Spring MVC下输出不同的格式--> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1"/> <property name="contentNegotiationManager" ref="contentNegotiationManager"/> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="order" value="0"/> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".vm"/> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <property name="contentType" value="text/html;charset=UTF-8"/> <property name="viewClass" value="springtry.web.util.VelocityToolbox2View"></property> <!--<property name="attributesMap"> <map> <entry key="sec"> <ref bean="velocitySecurityUtil"/> </entry> <entry key="req"> <ref bean="requestUtil"/> </entry> </map> </property>--> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> <property name="defaultViews"> <list> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> </list> </property> </bean> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="errors/error"/> <property name="exceptionMappings"> <props> <prop key="java.lang.Throwable">errors/error</prop> </props> </property> </bean> </beans>
详解:
1.<mvc:annotation-driven />标签,使用了比较重要的两个bean
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
解析类是org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser,通过使用
RootBeanDefinition handlerMappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
处理@RequestMapping,并将其注册到请求映射表中
通过使用
RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
...
this.addResponseBodyAdvice(handlerAdapterDef);
处理@Controller,确定调用哪个controller的哪个方法来处理当前请求
ps:网上说这个标签等同于单独定义这两个bean,我有异议,推荐同时使用,可以个性化定制RequestMappingHandlerMapping和RequestMappingHandlerAdapter
2.conversion-service="conversionService",上面的配置中使用了这项,什么意思呢?
提交到后台的数据通常都是字符串类型的,需要进行类型转换,这就是conversionService的作用了,发现一个问题,这个不是和RequestMappingHandlerAdapter定义的messageConverters冲突了么?没关系,因为messageConverters只针对requestBody(@RequestBody)
后面再详细介绍conversionService的各种类型转换
3.<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager"/>
以什么MediaTypes相应请求
4.<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
协商视图解析器