最近把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">

协商视图解析器

springmvc(1)--配置的更多相关文章

  1. SpringMVC、SpringMVC XML配置(纯XML方式)

    1.引入SrpingMVC所使用的Java包: cglib-nodep-2.1_3.jar.commons-logging.jar.spring-aspects-4.1.7.RELEASE.jar.s ...

  2. Springmvc中配置Quartz使用,实现任务实时调度。

    菜鸡的自我修炼,第一次接触quartz,做个记录.-------jstarseven 最近在项目中,第一次在springmvc中配置实用quartz,深刻的感受到quartz带来的方便,顺手做个记录. ...

  3. springMVC+Hibernate配置

    本文描述下 sypro 项目中使用 springMVC+Hibernate配置,初学SpringMVC做下简单整理解. 1.web项目首先我们要使用 web.xml文件将 spring配置引入进来 & ...

  4. SpringMVC简单配置

    SpringMVC简单配置 一.eclipse安装Spring插件 打开help下的Install New Software 点击add,location中输入http://dist.springso ...

  5. Idea简单SpringMVC框架配置

    前边已经介绍过了Struts在Idea上的配置,相对于Struts来说,我觉得SpringMVC有更多的优势,首先Struts是需要对action进行配置,页面发送不同的请求,就需要配置不同的acti ...

  6. SpringMVC常用配置(二),最简洁的配置实现文件上传

    Spring.SpringMVC持续介绍中,基础配置前面已经介绍了很多,如果小伙伴们还不熟悉可以参考这几篇文章: 1.Spring基础配置 2.Spring常用配置 3.Spring常用配置(二) 4 ...

  7. SpringMVC常用配置

    关于Spring.SpringMVC我们前面几篇博客都介绍了很多,但是还不够,这些框架中涉及到的注解.配置非常多,那么我们今天再来介绍一个SpringMVC的基本配置,灵活的使用这些配置,可以让我们在 ...

  8. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  9. maven+springmvc的配置

    1. 首先创建1个mavenweb项目  如果没有的话最好是去官网下载一个最新版本的eclipse  里面什么都有 maven/gradle 啥的 2. 选择路径   没啥影响 就是一个路径 默认就行 ...

  10. SpringMVC的配置和使用

    SpringMVC的配置和使用 什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于 ...

随机推荐

  1. mongo常用命令

    1.由于mongo没有关系型数据库常用,一些基础的命令容易忘记 db.table.update( { "_id" : xxx } , { $set : { "field1 ...

  2. 《dive into python3》 笔记摘录

    1.list can hold  arbitrary  objects and can expand dynamically as new items are added. A list is an  ...

  3. contentWindow 和contentDocument区别 及iframe访问

    a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...

  4. inpuy type=date

    http://www.w3schools.com/html/html_form_input_types.asp http://caniuse.com/#feat=input-datetime 浏览器兼 ...

  5. 关于request.getsession(true|false)

    request.getSession(true):若存在会话则返回该会话,否则新建一个会话.request.getSession(false):若存在会话则返回该会话,否则返回NULL

  6. JavaScript要点(十七) Math 对象

    来源:JavaScript 参考手册 Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). 语法 var ...

  7. 数据库相关文章转载(1) MySQL性能优化之参数配置

    1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...

  8. Apache与Tomcat 区别联系

    Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...

  9. socat : Some useful commands

    http://technostuff.blogspot.com/2008/10/some-useful-socat-commands.html MONDAY, OCTOBER 6, 2008 Some ...

  10. bzoj 1041: [HAOI2008]圆上的整点 数学

    1041: [HAOI2008]圆上的整点 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...