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

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 多视图配置(Jsp Freemarket Json)实例

    在这个"springMVC 多视图配置(Jsp Freemarket Json)实例"中,我们将探讨如何在 SpringMVC 中设置多种视图解析器来支持 Jsp、FreeMarker 和 Json 格式的响应。 **1. 视图解析器 (View Resolver)** 在 SpringMVC ...

    SpringMVC自定义多视图

    总的来说,自定义多视图是Spring MVC中增强灵活性和可扩展性的重要手段,它允许我们根据需求动态地选择和构造视图,从而满足不同用户和系统的交互需求。通过深入理解和实践,你可以有效地利用这一特性来优化你的Web...

    SpringMVC 使用JSON、XML视图

    在SpringMVC框架中,处理视图的呈现是至关重要的,尤其在当今Web应用程序中,JSON和XML格式的数据传输越来越普遍。SpringMVC为开发者提供了便捷的方式,将Java对象转换成JSON或XML视图,使得数据交换更加灵活。本文...

    SpringMVC第8讲:多视图支持

    SpringMVC还提供了ModelAndView类,用于在控制器中返回视图和模型数据。例如: ```java @RequestMapping("/example") public ModelAndView handleRequest() { Map, Object&gt; model = new HashMap(); model.put(...

    SpringMVC及视图解析器

    在 Spring 容器中,可以添加多个视图解析器,每个视图解析器都可以处理特定的视图类型。例如,InternalResourceViewResolver 可以处理 JSP 视图。 四、配置 在 SpringMVC 中,需要在 web.xml 文件中声明 SpringMVC...

    Spring MVC--5.视图和视图解析器

    在Spring MVC框架中,视图和视图解析器是实现MVC模式中“V”(View)部分的关键组件。它们负责将处理完的数据转化为用户可以理解的格式,并展示在浏览器上。视图解析器则是连接控制器与视图的桥梁,它负责根据返回的...

    SpringMVC PPT_springmvc_

    SpringMVC 提供了对文件上传和下载的支持,可以方便地处理用户的文件操作请求。 十一、RESTful API 设计 SpringMVC 也支持 RESTful 风格的 Web 服务,可以通过 @RequestMapping 注解配合 HTTP 方法(GET、POST、PUT...

    (代码)SpringMVC第8讲:多视图支持

    SpringMVC作为一个强大的MVC框架,提供了丰富的功能来处理多种类型的视图技术,使得开发者能够灵活地选择最适合项目需求的视图层实现。 首先,我们来看一下SpringMVC中的DispatcherServlet,它是所有请求的入口点。...

    springMVC配置好的环境

    在配置好 Spring MVC 环境后,开发者可以专注于业务逻辑的实现,而不必关心底层的请求处理和视图渲染细节。这样的环境通常意味着所有必要的库、配置和集成都已经到位,可以直接进行开发工作。在开发过程中,开发者应...

    java springmvc

    SpringMVC通过解耦控制器、业务逻辑和服务层,使得开发者可以更专注于应用程序的核心功能。下面将详细讨论SpringMVC的关键组件和它们在实际开发中的应用。 1. **DispatcherServlet**: SpringMVC的核心组件,作为...

    SpringMVC 入门小程序

    本入门程序旨在帮助初学者理解并掌握SpringMVC的基本概念和工作流程,通过实现用户在前端页面注册信息并显示的功能,来深入剖析其核心机制。 ### 1. SpringMVC框架概述 SpringMVC是Spring框架的一部分,提供了一个...

    SpringMvc学习笔记

    Spring MVC与Struts2等同属表现层框架,它们的主要任务是协调控制器、模型和视图,处理用户请求并展示结果。在Spring框架的体系中,Spring MVC提供了高度的可扩展性和灵活性。 2. **工作流程** - 用户发起请求...

    mybatis 和springmvc整合

    而SpringMVC则是Spring框架的一个模块,专门用于构建Web应用,提供模型视图控制器的处理机制,实现了请求和业务逻辑的解耦。 整合MyBatis和SpringMVC,首先需要在项目中引入这两个框架的依赖。在 Maven 或 Gradle ...

    SpringMVC完整使用教程

    综上所述,SpringMVC 提供了一个强大的 MVC 框架,涵盖了请求处理、参数绑定、模型数据管理、视图解析、异常处理等多个方面,极大地提高了开发效率和代码质量。通过深入学习和实践,开发者可以更好地利用 SpringMVC ...

    SpringMVC详细文档,内容通俗易懂,适合学习springMVC的初学者

    总结起来,SpringMVC是Java Web开发中一种强大且灵活的框架,它简化了MVC模式的实现,提供了组件化的结构,使得开发者可以专注于业务逻辑的实现,而不必关心底层的请求处理和视图渲染细节。结合MyBatis等其他框架,...

    springMVC练手代码

    这些配置会指导SpringMVC如何处理请求和响应。 3. **模型(Model)**:模型通常由业务对象(BO)或数据访问对象(DAO)组成,它们持有应用程序的数据。在练习代码中,你可能看到与数据库交互的实体类和DAO接口。 4. ...

    SpringMvc教程 跟着我学SpringMVC

    SpringMVC是Spring框架的一个模块,它是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过提供高度可配置的途径支持以面向切面编程(AOP)来管理业务对象。在学习这本书的过程中,我们将接触到...

    springMVC教案.rar

    SpringMVC允许定义全局或特定类型的异常处理器,通过@ControllerAdvice和@ExceptionHandler注解实现。 8. **视图解析**: 视图解析器如InternalResourceViewResolver,负责根据视图名查找实际的视图资源。例如,...

Global site tag (gtag.js) - Google Analytics