`
liudaowo
  • 浏览: 4293 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

SpringMVC多请求类型和多视图

阅读更多

这份只是我的留下以后作参考的,其中有些代码、注释如有不对,请各位大牛指出,小弟不胜感激。
<?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:p="http://www.springframework.org/schema/p"
       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.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
			http://www.springframework.org/schema/mvc 
			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--		<context:spring-configured/>-->
		<context:component-scan base-package="cn.ksms"/>
<!--		<context:component-scan base-package="cn.ksms.controller"/>-->
		
		<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
		
		<bean id="feermarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
			<!-- 模板本地路径 -->
			<property name="templateLoaderPath" value="/ftl/"></property>
			<property name="freemarkerSettings">
				<props>
					<!-- 配置模板字符集编码 -->
					<prop key="defaultEncoding">UTF-8</prop>
					<!-- 设置每隔多少秒去检查模板是否被更新,按秒算 -->
					<prop key="template_update_delay">0</prop>
					<!-- 本地语言,中文 -->
					<prop key="locale">zh_CN</prop>
				</props>
			</property>
			<!-- 一些freemarker的扩展,或者用户自定义方法 -->
			<property name="freemarkerVariables">
				<map>
					<!-- 支持#escape指令:将<,>,$,",\转义为html符号&lt,&gt,&amp,&quot,&apos -->
	      			<entry key="xml_escape" value-ref="fmXmlEscape"/>
	      			<!-- 支持@block指令:定义块,可以被子模板用@override指令覆盖显示 -->
	      			<entry key="block" value-ref="block"/>
	      			<!-- 支持@override指令:覆盖@block指令显示的内容 -->
	      			<entry key="override" value-ref="override"/>
	      			<!-- 支持@extends指令:继承其他模板,必须放在模板的最后面(该指令完全等价于#include指令,只是为了提供统一的语义,即extends比include更好理解) -->
	      			<entry key="extends" value-ref="extends"/>	      			
	   	 		</map>
			</property>
		</bean>
		<!-- 具体的类 -->
		<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
		<!-- 下面三个类实现了模板的继承功能 -->
		<bean id="block" class="cn.org.rapid_framework.freemarker.directive.BlockDirective"></bean>
		<bean id="override" class="cn.org.rapid_framework.freemarker.directive.OverrideDirective"></bean>
		<bean id="extends" class="cn.org.rapid_framework.freemarker.directive.ExtendsDirective"></bean>
		
		<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
			<property name="messageConverters">
				<list>
					<!-- 解析json请求数据,将json转换为java对象 -->
					<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
						<property name="supportedMediaTypes">
							<list>
								<value>text/html;charset=UTF-8</value>
							</list>
						</property>
					</bean>
					<!-- 解析xml请求数据,将xml转换为java对象 -->
					<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
						<constructor-arg>
							<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
								<property name="streamDriver">
									<bean class="com.thoughtworks.xstream.io.xml.DomDriver"/>
								</property>
								
								<property name="autodetectAnnotations" value="true"></property>
								<!-- 可以与xml互换的对象,需要使用xstream的注解,注解的使用方法请参xstream官网 -->
								<property name="annotatedClasses">
									<list>
										<value>cn.ksms.pojo.Message</value>
										<value>cn.ksms.pojo.Parentinfo</value>
									</list>
								</property>
							</bean>
						</constructor-arg>
					</bean>
					<bean class="org.springframework.http.converter.FormHttpMessageConverter"></bean>
					<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"></bean>
					<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
					<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
					<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
				</list>
			</property>
		</bean>
		
<!--		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--			<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
<!--			<property name="prefix" value="/jsp/"></property>-->
<!--			<property name="suffix" value=""></property>-->
<!--		</bean>-->
		
		<!-- 本身并不去解析,只是分配给其他的ViewResolver -->
		 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		 	<property name="defaultContentType" value="application/json"></property>
		 	<property name="mediaTypes">
		 		<map>
		 			<entry key="html" value="text/html"></entry>
		 			<entry key="json" value="application/json"></entry>
		 			<entry key="xml" value="application/xml"></entry>
		 		</map>
		 	</property>
		 	<!-- 返回视图解析器 -->
		 	<property name="viewResolvers">
		 		<list>
		 			<!-- 配置freeMarker视图解析器 -->
		 			<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		 				<!-- 启用缓存,按字面意思猜的 -->
		 				<property name="cache" value="true"></property>
		 				<property name="prefix" value=""></property>
		 				<property name="suffix" value=".ftl"></property>
		 				<!-- 同exposeSessionAttributes-->
		 				<property name="exposeSpringMacroHelpers" value="true"></property>
		 				<!-- 同exposeSessionAttributes-->
		 				<property name="exposeRequestAttributes" value="true"></property>
		 				<!-- 是否所有session属性在于模板进行合并之前添加到model中,可以理解为session范围内所包含的所有对象,而不是一个真正的session对象 -->
		 				<property name="exposeSessionAttributes" value="true"></property>
		 				<property name="contentType" value="text/html;charset=UTF-8"></property>
		 			</bean>
		 			<!-- 配置jsp的视图解析器 -->
		 			<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		 				<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		 				<property name="prefix" value="/jsp/"/>
		 				<property name="suffix" value=""></property>
		 			</bean>
		 		</list>
		 	</property>
		 	<!-- 默认返回视图 -->
		 	<property name="defaultViews">
		 		<list>
		 			<!-- 输出为JSON数据 -->
		 			<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"></bean>
		 			<!-- 输出为xml数据 -->
		 			<bean id="marshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
		 				<property name="marshaller">
		 					<bean id="XStreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
		 						<property name="autodetectAnnotations" value="true"></property>
		 					</bean>
		 				</property>
		 				<property name="contentType" value="application/xml"></property>
		 			</bean>
		 		</list>
		 	</property>
		 </bean>
		 
		<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
			<property name="defaultErrorView" value="/error.jsp"></property>
			
			<property name="exceptionMappings">
				<props>
					<prop key="java.sql.SQLException">errorDB.jsp</prop>
					<prop key="java.lang.RuntimeException">errorRT.jsp</prop>
				</props>
			</property>
		</bean>
		<!-- 拦截器 -->
		<mvc:interceptors>
<!--			<mvc:interceptor>-->
<!--				<mvc:mapping path="/SeriviceProcessor"/>-->
<!--				<bean class="cn.ksms.interceptor.XmlInterceptor"></bean>-->
<!--			</mvc:interceptor>-->
<!--			<mvc:interceptor>-->
<!--				<mvc:mapping path="/*"/>-->
				<bean class="cn.ksms.interceptor.MyInterceptor"></bean>		
<!--			</mvc:interceptor>-->
		</mvc:interceptors>
		<!-- 静态文件加载 -->
		<mvc:resources location="/js/" mapping="/js/**"/>
		<mvc:resources location="/image/" mapping="/image/**"/>
		<mvc:resources location="/css/" mapping="/css/**"/>
		<mvc:resources location="/res/" mapping="/res/**"/>
</beans>
 
分享到:
评论

相关推荐

    SpringMVC笔记.pdf

    SpringMVC提供了文件上传和下载的功能,可以通过使用multipart/form-data类型的请求体来上传文件。 十一、拦截器 SpringMVC提供了拦截器的功能,可以用于拦截请求和响应。 十二、异常处理器 SpringMVC提供了...

    springMVC学习

    本指南提供了 SpringMVC 的核心思想、框架特点、入门示例、参数传递、标签介绍、拦截器、类型转换、JSON 格式数据的输入和输出、文件上传、国际化和本地化、验证等方面的知识点,旨在帮助开发者快速掌握 SpringMVC ...

    0325_SpringMVC.html

    * 4)、来看请求地址和@RequestMapping标注的哪个匹配,来找到到底使用那个类的哪个方法来处理 * 5)、前端控制器找到了目标处理器类和目标方法,直接利用返回执行目标方法; * 6)、方法执行完成以后会有一个...

    SpringMVC面试专题.pdf

    18、SpringMvc 中有个类把视图和数据都合并的一起的,叫什么? 19、怎么样把 ModelMap 里面的数据放入 Session 里面? 20、SpringMvc 怎么和 AJAX 相互调用的? 21、当一个方法向 AJAX 返回特殊对象,譬如 Object,List...

    SpringMVC实现笔记_SpringMVC实现笔记_

    开发步骤①导入SpringMVC相关坐标②配置SpringMVC核心控制器DispathcerServlet③创建Controller类和视图页面④使用注解配置Controller类中业务方法的映射地址⑤配置SpringMVC核心文件 spring-mvc.xml⑥客户端发起...

    Spring SpringMVC 简单整合

    Spring SpringMVC 简单整合(初学者参考) demo项目对应地址说明 :...ViewResolver:通过扩展视图解析器,支持更多类型的视图解析,例如:jsp、freemarker、pdf、excel等。

    SpringMVC框架架构介绍

    一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心...十八、spring mvc 多视图控制器 十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作 二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    springmvc思维导图

    springmvc的概念、springmvc的运行原理(前端控制器、处理器映射器、处理器适配器、处理器、视图解析器)、同步请求的方式(ModelAndView、String、无返回值、默认的绑定值)、异步请求的注解、请求参数的封装(简单...

    springMVC rest风格视图解析

    springMVC spring mybatis rest风格架构 根据请求的后缀名 解析成json 或者 xml格式的数据

    SpringMVC4超权威教程

    1.SpringMVC 概述 2.SpringMVC 的 HelloWorld 3.使用 @RequestMapping 映射请求 ...视图和视图解析器 7.RESTful CRUD • 8.SpringMVC 表单标签 &处理静态资源 • 9.数据转换 & 数据格式化 & 数据校验

    springmvc demo

    Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发...

    SpringMVC讲义大全.docx

    SpringMVC框架概述: 介绍什么是SpringMVC,它的优势和特点,以及它在Web应用开发中的作用。 SpringMVC基本原理: 解释SpringMVC框架的工作原理,包括前端控制器、处理器映射器、处理器适配器、视图解析器等核心...

    springmvc开发资料

    SpringMVC流程 1、 用户发送请求至前端控制器DispatcherServlet。 2、 DispatcherServlet收到请求调用HandlerMapping处理器映射器。 3、 处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成...

    SpringMvc源码

    Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发...

    SpringMVCC.txt

    SpringMVC是一个模型 - 视图 - 控制器(MVC)的Web框架建立在中央前端控制器servlet(DispatcherServlet),它负责发送每个请求到合适的处理程序,使用视图来最终返回响应结果的概念。Spring MVC 是 Spring 产品组合...

    springmvc第一天.pdf

    SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架, 属于 Spring FrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring 框架提供了构建 Web 应用程序的全功 能 MVC 模块。...

    SpringMVC示例

    本次实践内容包括RequestMapping关键字修饰类和方法(请求方式、请求参数&请求头、Ant风格路径)、PathVariable注解、HiddenHttpMethodFilter 过滤器(将Get请求转换成PUT、DELETE请求)、 RequestParam 注解、...

    23道SpringMVC常见面试题.docx

    全部的应用对象,无论控制器和视图,还是业务对象之类的都是 java 组件.并且和 Spring 提供的其他基础结构紧密集成. 2) 不依赖于 Servlet API(目标虽是如此,但是在实现的时候确实是依赖于 Servlet 的) 3) 可以任意...

    springMVC 详细版本 拦截器源码

    springMVC 拦截器源代码 内有详细文档介绍操作 @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { //可以通过...

    springmvc第二天.pdf

    1.2. Springmvc 优点 1、清晰的角色划分: 前端控制器(DispatcherServlet) 请求到处理器映射(HandlerMapping) 处理器适配器(HandlerAdapter) 视图解析器(ViewResolver) 处理器或页面控制器(Controller) ...

Global site tag (gtag.js) - Google Analytics