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">
协商视图解析器
springmvc(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 ...
- Springmvc中配置Quartz使用,实现任务实时调度。
菜鸡的自我修炼,第一次接触quartz,做个记录.-------jstarseven 最近在项目中,第一次在springmvc中配置实用quartz,深刻的感受到quartz带来的方便,顺手做个记录. ...
- springMVC+Hibernate配置
本文描述下 sypro 项目中使用 springMVC+Hibernate配置,初学SpringMVC做下简单整理解. 1.web项目首先我们要使用 web.xml文件将 spring配置引入进来 & ...
- SpringMVC简单配置
SpringMVC简单配置 一.eclipse安装Spring插件 打开help下的Install New Software 点击add,location中输入http://dist.springso ...
- Idea简单SpringMVC框架配置
前边已经介绍过了Struts在Idea上的配置,相对于Struts来说,我觉得SpringMVC有更多的优势,首先Struts是需要对action进行配置,页面发送不同的请求,就需要配置不同的acti ...
- SpringMVC常用配置(二),最简洁的配置实现文件上传
Spring.SpringMVC持续介绍中,基础配置前面已经介绍了很多,如果小伙伴们还不熟悉可以参考这几篇文章: 1.Spring基础配置 2.Spring常用配置 3.Spring常用配置(二) 4 ...
- SpringMVC常用配置
关于Spring.SpringMVC我们前面几篇博客都介绍了很多,但是还不够,这些框架中涉及到的注解.配置非常多,那么我们今天再来介绍一个SpringMVC的基本配置,灵活的使用这些配置,可以让我们在 ...
- SpringMVC基础配置(通过注解配置,非xml配置)
SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...
- maven+springmvc的配置
1. 首先创建1个mavenweb项目 如果没有的话最好是去官网下载一个最新版本的eclipse 里面什么都有 maven/gradle 啥的 2. 选择路径 没啥影响 就是一个路径 默认就行 ...
- SpringMVC的配置和使用
SpringMVC的配置和使用 什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于 ...
随机推荐
- centOS安装openoffice
centOS安装openoffice的方法: yum install openoffice.org-writer yum install openoffice.org-calc yum install ...
- 从零新建一个winform项目
网站:https://community.devexpress.com/blogs/eaf/archive/2012/10/30/xaf-application-from-scratch.aspx
- Spring 常用工具类
1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...
- CloudStack4.2 更新全局参数
{ "updateconfigurationresponse": { "configuration": { "category": &quo ...
- DIV+CSS规范命名大全集合
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-19) 网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DI ...
- CSS(04) 定位
布局常用的三种:标准流.定位.浮动: 1.文档流-标准流 窗体自上而下分成一行行(元素在 (X)HTML 中的位置),并在一行行中从左到右排放元素: 2.CSS 定位 Position 属性(绝对定位 ...
- .NET正则基础——.NET正则类及方法应用
1 概述 初学正则时,对于Regex类不熟悉,遇到问题不知道该用哪种方法解决,本文结合一些正则应用的典型应用场景,介绍一下Regex类的基本应用.这里重点进行.NET类的介绍,对于正则的 ...
- Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果
如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 某天看到京东商城首页的滑动广告的Banner,在流动切换的时候有立体的动画效果,感觉很有意思,然后研究了下 ...
- C++ Code_StatusBar
主题 1. 创建状态栏 并显示 2. 在状态栏中显示进度条 3. MDI文档显示和隐藏状态栏 4. 5. 代码::创建状态栏 并显示 //手动添加3个ICON //////////////// ...
- 全面整理的C++面试题
C++面试题 1.是不是一个父类写了一个virtual 函数,假设子类覆盖它的函数不加virtual ,也能实现多态? virtual修饰符会被隐形继承的. private 也被集成,仅仅事派生类没有 ...