Spring MVC 教程,快速入门,深入分析-[12-20]
十二、如何把全局异常记录到日志中?
在前的配置中,其中有一个属性warnLogCategory,值是“SimpleMappingExceptionResolver类的全限定名”。我是在SimpleMappingExceptionResolver类父类AbstractHandlerExceptionResolver类中找到这个属性的。查看源码后得知:如果warnLogCategory不为空,spring就会使用apache的org.apache.commons.logging.Log日志工具,记录这个异常,级别是warn。
值:“org.springframework.web.servlet.handler.SimpleMappingExceptionResolver”,是“SimpleMappingExceptionResolver类的全限定名”。这个值不是随便写的。 因为我在log4j的配置文件中还要加入log4j.logger.org.springframework.web.servlet.handler.SimpleMappingExceptionResolver=WARN,保证这个级别是warn的日志一定会被记录,即使log4j的根日志级别是ERROR。
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十三、如何给spring3 MVC中的Action做JUnit单元测试?
使用了spring3 MVC后,给action做单元测试变得很方便,我以前从来不给action写单元测试的,现在可以根据情况写一些了。
不用给每个Action都写单元测试吧,自己把握吧。
JUnitActionBase类是所有JUnit的测试类的父类
Java代码
package test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
/**
* 说明: JUnit测试action时使用的基类
*
* @author 赵磊
* @version 创建时间:2011-2-2 下午10:27:03
*/
public class JUnitActionBase {
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
/**
* 读取spring3 MVC配置文件
*/
@BeforeClass
public static void setUp() {
if (handlerMapping == null) {
String[] configs = { "file:src/springConfig/springMVC.xml" };
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc); context.refresh();
msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context.getBean(context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
}
/**
* 执行request对象请求的action
*
* @param request
* @param response
* @return
* @throws Exception
*/
public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response)
throws Exception {
HandlerExecutionChain chain = handlerMapping.getHandler(request);
final ModelAndView model = handlerAdapter.handle(request, response,
chain.getHandler());
return model;
}
}
Java代码
package test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
/**
* 说明: JUnit测试action时使用的基类
*
* @author 赵磊
* @version 创建时间:2011-2-2 下午10:27:03
*/
public class JUnitActionBase {
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
/**
* 读取spring3 MVC配置文件
*/
@BeforeClass
public static void setUp() {
if (handlerMapping == null) {
String[] configs = { "file:src/springConfig/springMVC.xml" };
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc); context.refresh();
msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context.getBean(context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
}
/**
* 执行request对象请求的action
*
* @param request
* @param response
* @return
* @throws Exception
*/
public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response)
throws Exception {
HandlerExecutionChain chain = handlerMapping.getHandler(request);
final ModelAndView model = handlerAdapter.handle(request, response,
chain.getHandler());
return model;
}
}
这是个JUnit测试类,我们可以new Request对象,来参与测试,太方便了。给request指定访问的URL,就可以请求目标Action了。
Java代码
package test.com.app.user;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import test.JUnitActionBase;
/**
* 说明: 测试OrderAction的例子
*
* @author 赵磊
* @version 创建时间:2011-2-2 下午10:26:55
*/
public class TestOrderAction extends JUnitActionBase {
@Test
public void testAdd() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setServletPath("/order/add");
request.addParameter("id", "1002");
request.addParameter("date", "2010-12-30");
request.setMethod("POST");
// 执行URI对应的action
final ModelAndView mav = this.excuteAction(request, response);
// Assert logic
Assert.assertEquals("order/add", mav.getViewName());
String msg=(String)request.getAttribute("msg");
System.out.println(msg);
}
}
Java代码
package test.com.app.user;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import test.JUnitActionBase;
/**
* 说明: 测试OrderAction的例子
*
* @author 赵磊
* @version 创建时间:2011-2-2 下午10:26:55
*/
public class TestOrderAction extends JUnitActionBase {
@Test
public void testAdd() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setServletPath("/order/add");
request.addParameter("id", "1002");
request.addParameter("date", "2010-12-30");
request.setMethod("POST");
// 执行URI对应的action
final ModelAndView mav = this.excuteAction(request, response);
// Assert logic
Assert.assertEquals("order/add", mav.getViewName());
String msg=(String)request.getAttribute("msg");
System.out.println(msg);
}
}
需要说明一下 :由于当前最想版本的Spring(Test) 3.0.5还不支持@ContextConfiguration的注解式context file注入,所以还需要写个setUp处理下,否则类似于Tiles的加载过程会有错误,因为没有ServletContext。3.1的版本应该有更好的解决方案,
参见: https://jira.springsource.org/browse/SPR-5243 。
参考 :http://www.iteye.com/topic/828513
十四、转发与重定向
可以通过redirect/forward:url方式转到另一个Action进行连续的处理。
可以通过redirect:url 防止表单重复提交 。
写法如下:
return "forward:/order/add";
return "redirect:/index.jsp";
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
带参数重定向--RedirectAttributes
用户保存或修改后,为了防止用户刷新浏览器(F5)导致表单重复提交,一般在保存或修改操作之后会redirect到一个结果页面(不是forward),同时携带参数,如操作成功的提示信息。因为是Redirect,Request里的attribute不会传递过去。Spring在3.1才提供了这个能力--RedirectAttributes。 反复按F5,操作成功的提示信息也不会再次出来(总共只出现一次),效果很理想。
Java代码
public String save(@ModelAttribute("group") Group group, RedirectAttributes redirectAttributes) {
accountManager.saveGroup(group);
redirectAttributes.addFlashAttribute("message", "操作成功");
return "redirect:/account/group/";
}
Java代码
public String save(@ModelAttribute("group") Group group, RedirectAttributes redirectAttributes) {
accountManager.saveGroup(group);
redirectAttributes.addFlashAttribute("message", "操作成功");
return "redirect:/account/group/";
}
十五、处理ajax请求
1、引入下面两个jar包,我用的是1.7.2,好像1.4.2版本以上都可以,下载地址:http://wiki.fasterxml.com/JacksonDownload
jackson-core-asl-1.7.2.jar
jackson-mapper-asl-1.7.2.jar
2、spring的配置文件中要有这一行,才能使用到spring内置支持的json转换。如果你手工把POJO转成json就可以不须要使用spring内置支持的json转换。
<mvc:annotation-driven />
3、使用@ResponseBody注解
Java代码
/**
* ajax测试
* http://127.0.0.1/mvc/order/ajax
*/
@RequestMapping("/ajax")
@ResponseBody
public Object ajax(HttpServletRequest request){
List<String> list=new ArrayList<String>();
list.add("电视");
nbsp; list.add("洗衣机");
list.add("冰箱");
list.add("电脑");
list.add("汽车");
list.add("空调");
list.add("自行车");
list.add("饮水机");
list.add("热水器");
return list;
}
Java代码
/**
* ajax测试
* http://127.0.0.1/mvc/order/ajax
*/
@RequestMapping("/ajax")
@ResponseBody
public Object ajax(HttpServletRequest request){
List<String> list=new ArrayList<String>();
list.add("电视");
nbsp; list.add("洗衣机");
list.add("冰箱");
list.add("电脑");
list.add("汽车");
list.add("空调");
list.add("自行车");
list.add("饮水机");
list.add("热水器");
return list;
}
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十六、关于写几个配置文件的说明
我看到有的人把配置文件写两份:
一个是原有的applicationContext.xml,这个文件从spring2.0-2.5时一直在使用。
别一个是新加的spring MVC的配置文件。
其实这两个文件是可以写成一个文件的,springMVC相关的配置,数据源,事务相关配置可以都写再一个配置文件中。
本例子中只使用了一个spring配置文件叫“springMVC.xml”。
就不要再多配置一个applicationContext.xml文件了。
web.xml文件中也不要再配置org.springframework.web.context.ContextLoaderListener的listener了。
写两个配置文件一般就会导致扫描两次,一定要精确控制扫描的包名,做到不重复扫描。
写两个配置文件还出现事务不好使的现象,是当把@Transactional写有Action层时出现的。
是因为父子上下文的原因,请参看前的 第五节 父子上下文,里面有说明 。原因是父上下文不能访问子上下文。
十七、如何取得Spring管理的bean (请用第3种方法)
1、servlet方式加载时,
【web.xml】
Xml代码
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Xml代码
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC
注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。
Java代码
ServletContext sc=略
WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
Java代码
ServletContext sc=略
WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
2、listener方式加载时:
【web.xml】
Xml代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Xml代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
【jsp/servlet】可以这样取得
Java代码
ServletContext context = getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);
Java代码
ServletContext context = getServletContext();
WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);
3、通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。
在配置文件中加入:
Xml代码
<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
<bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />
Xml代码
<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring bean对象 -->
<bean class="com.xxxxx.SpringContextHolder" lazy-init="false" />
Java代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
Java代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
*
*/
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext; // NOSONAR
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}
/**
* 清除applicationContext静态变量.
*/
public static void cleanApplicationContext() {
applicationContext = null;
}
private static void checkApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十八、多视图控制器
当有jsp,flt (模板)等多种页面生成展示方式时,spring默认使用的是“视图解析器链”。 真是一个链,所以性能不好,spring会在“视图解析器链”中顺序的查找,直到找到对应的 “视图解析器” 。jsp视图解析器一定要写在最后面,因为一旦调用jsp,就向浏览器发出数据了,Spring就没有机会再尝试下一个了。
所以自己写一个"多视图解析器",依靠扩展名来区分,可一次准确的选中一个 视图解析器,提高性能(会有多少提高呢?没测试过).
下面的例子支持jsp,flt (模板)两种页面生成展示方式,你中以自己添加,支持更多。
Xml代码
<!-- 多视图处理器 -->
<bean class="com.xxx.core.web.MixedViewResolver">
<property name="resolvers">
<map>
<entry key="jsp">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
</entry>
<entry key="ftl">
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="contentType" value="text/html;charset=UTF-8"></property>
<!-- 宏命令的支持 -->
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="requestContextAttribute" value="rc"></property>
</bean>
</entry>
</map>
</property>
</bean>
<!-- freemarker config -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
Xml代码
<!-- 多视图处理器 -->
<bean class="com.xxx.core.web.MixedViewResolver">
<property name="resolvers">
<map>
<entry key="jsp">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
</entry>
<entry key="ftl">
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="contentType" value="text/html;charset=UTF-8"></property>
<!-- 宏命令的支持 -->
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="requestContextAttribute" value="rc"></property>
</bean>
</entry>
</map>
</property>
</bean>
<!-- freemarker config -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
Java代码
import java.util.Locale;
import java.util.Map;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
/**
* 说明: 多视图处理器
*
* @author 赵磊
* @version 创建时间:2011-8-19 上午09:41:09
*/
public class MixedViewResolver implements ViewResolver{
private Map<String,ViewResolver> resolvers;
public void setResolvers(Map<String, ViewResolver> resolvers) {
this.resolvers = resolvers;
}
public View resolveViewName(String viewName,Locale locale) throws Exception{
int n=viewName.lastIndexOf(".");
if(n!=-1){
//取出扩展名
String suffix=viewName.substring(n+1);
//取出对应的ViewResolver
ViewResolver resolver=resolvers.get(suffix);
if(resolver==null){
throw new RuntimeException("No ViewResolver for "+suffix);
}
return resolver.resolveViewName(viewName, locale);
}else{
ViewResolver resolver=resolvers.get("jsp");
return resolver.resolveViewName(viewName, locale);
}
}
}
Java代码
import java.util.Locale;
import java.util.Map;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
/**
* 说明: 多视图处理器
*
* @author 赵磊
* @version 创建时间:2011-8-19 上午09:41:09
*/
public class MixedViewResolver implements ViewResolver{
private Map<String,ViewResolver> resolvers;
public void setResolvers(Map<String, ViewResolver> resolvers) {
this.resolvers = resolvers;
}
public View resolveViewName(String viewName,Locale locale) throws Exception{
int n=viewName.lastIndexOf(".");
if(n!=-1){
//取出扩展名
String suffix=viewName.substring(n+1);
//取出对应的ViewResolver
ViewResolver resolver=resolvers.get(suffix);
if(resolver==null){
throw new RuntimeException("No ViewResolver for "+suffix);
}
return resolver.resolveViewName(viewName, locale);
}else{
ViewResolver resolver=resolvers.get("jsp");
return resolver.resolveViewName(viewName, locale);
}
}
}
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
十九、 <mvc:annotation-driven /> 到底做了什么工作
一句 <mvc:annotation-driven />实际做了以下工作:(不包括添加自己定义的拦截器)
我们了解这些之后,对Spring3 MVC的控制力就更强大了,想改哪就改哪里。
Xml代码
<!-- 注解请求映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="logNDCInteceptor"/> <!-- 日志拦截器,这是你自定义的拦截器 -->
<ref bean="myRequestHelperInteceptor"/> <!-- RequestHelper拦截器,这是你自定义的拦截器-->
<ref bean="myPermissionsInteceptor"/> <!-- 权限拦截器,这是你自定义的拦截器-->
<ref bean="myUserInfoInteceptor"/> <!-- 用户信息拦截器,这是你自定义的拦截器-->
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="byteArray_hmc" />
<ref bean="string_hmc" />
<ref bean="resource_hmc" />
<ref bean="source_hmc" />
<ref bean="xmlAwareForm_hmc" />
<ref bean="jaxb2RootElement_hmc" />
<ref bean="jackson_hmc" />
</list>
</property>
</bean>
<bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 处理.. -->
<bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!-- 处理.. -->
<bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 处理.. -->
<bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 处理.. -->
<bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 处理.. -->
<bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 处理.. -->
<bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 处理json-->
Xml代码
<!-- 注解请求映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="logNDCInteceptor"/> <!-- 日志拦截器,这是你自定义的拦截器 -->
<ref bean="myRequestHelperInteceptor"/> <!-- RequestHelper拦截器,这是你自定义的拦截器-->
<ref bean="myPermissionsInteceptor"/> <!-- 权限拦截器,这是你自定义的拦截器-->
<ref bean="myUserInfoInteceptor"/> <!-- 用户信息拦截器,这是你自定义的拦截器-->
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="byteArray_hmc" />
<ref bean="string_hmc" />
<ref bean="resource_hmc" />
<ref bean="source_hmc" />
<ref bean="xmlAwareForm_hmc" />
<ref bean="jaxb2RootElement_hmc" />
<ref bean="jackson_hmc" />
</list>
</property>
</bean>
<bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 处理.. -->
<bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!-- 处理.. -->
<bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!-- 处理.. -->
<bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!-- 处理.. -->
<bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!-- 处理.. -->
<bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!-- 处理.. -->
<bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!-- 处理json-->
转载请注明出处:原文地址:http://elf8848.iteye.com/blog/875830
二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址
要在http://www.iteye.com/网站有注册帐号才能下载(这不能怪我)
Spring_MVC_教程_快速入门_深入分析V1.1.pdf
SpringMVC核心配置文件示例.rar
Spring_MVC_教程_快速入门_深入分析V1.1.pdf (706.2 KB)
下载次数: 14466
SpringMVC核心配置文件示例.rar (2.2 KB)
下载次数: 8395
查看图片附件