Spring MVC 教程,快速入门,深入分析(5)

2019-03-03 23:09

11. 12.

这里主要的类是SimpleMappingExceptionResolver类,和他的父类AbstractHandlerExceptionResolver类。

具体可以配置哪些属性,我是通过查看源码知道的。

你也可以实现HandlerExceptionResolver接口,写一个自己的异常处理程序。spring的扩展性是很好的。

通过SimpleMappingExceptionResolver我们可以将不同的异常映射到不同的jsp页面(通过exceptionMappings属性的配置)。

同时我们也可以为所有的异常指定一个默认的异常提示页面(通过

defaultErrorView属性的配置),如果所抛出的异常在exceptionMappings中没有对应的映射,则Spring将用此默认配置显示异常信息。

注意这里配置的异常显示界面均仅包括主文件名,至于文件路径和后缀已经在viewResolver中指定。如/error/error表示/error/error.jsp

显示错误的jsp页面:

Html代码

1. <%@ page language=\ contentType=\

\

2. pageEncoding=\%>

3. <%@ page import=\%>

4.

N\>

5. 6.

7.

GBK\>

8. 错误页面 9. 10.

11.

出错了

12. <%

13. Exception e = (Exception)request.getAttribute(\

\

14. out.print(e.getMessage()); 15. %>

16. 17.

其中一句:request.getAttribute(\,key是exception,也是在SimpleMappingExceptionResolver类默认指定的,是可能通过配置文件修改这个值的,大家可以去看源码。

十二、如何把全局异常记录到日志中?

在前的配置中,其中有一个属性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代码

1. 2. 3. 4. 5. 6.

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;

tionContext;

7. import org.springframework.web.context.support.XmlWebApplica8. import org.springframework.web.servlet.HandlerAdapter; 9. import org.springframework.web.servlet.HandlerExecutionChai

n;

10. import org.springframework.web.servlet.HandlerMapping; 11. import org.springframework.web.servlet.ModelAndView;

12. import org.springframework.web.servlet.mvc.annotation.Annota

tionMethodHandlerAdapter;

13. import org.springframework.web.servlet.mvc.annotation.Defaul

tAnnotationHandlerMapping;

14. /**

15. * 说明: JUnit测试action时使用的基类 16. *

17. * @author 赵磊

18. * @version 创建时间:2011-2-2 下午10:27:03 19. */

20. public class JUnitActionBase {

21. private static HandlerMapping handlerMapping; 22. private static HandlerAdapter handlerAdapter; 23. /**

24. * 读取spring3 MVC配置文件 25. */

26. @BeforeClass

27. public static void setUp() {

28. if (handlerMapping == null) {

29. String[] configs = { \

ngMVC.xml\ };

30. XmlWebApplicationContext context = new XmlWebApp

licationContext();

31. context.setConfigLocations(configs);

32. MockServletContext msc = new MockServletContext

();

33. context.setServletContext(msc); context.

refresh();

34. msc.setAttribute(WebApplicationContext.ROOT_WEB_

APPLICATION_CONTEXT_ATTRIBUTE, context);

35. handlerMapping = (HandlerMapping) context

36. .getBean(DefaultAnnotationHandlerMapping.

class);

37. handlerAdapter = (HandlerAdapter) context.getBea

n(context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);

38. } 39. } 40.

41. /**

42. * 执行request对象请求的action 43. *

44. * @param request 45. * @param response 46. * @return

47. * @throws Exception 48. */

49. public ModelAndView excuteAction(HttpServletRequest requ

est, HttpServletResponse response)

50. throws Exception {

51. HandlerExecutionChain chain = handlerMapping.getHand

ler(request);

52. final ModelAndView model = handlerAdapter.handle(req

uest, response,

53. chain.getHandler()); 54. return model; 55. } 56. }

这是个JUnit测试类,我们可以new Request对象,来参与测试,太方便了。给request指定访问的URL,就可以请求目标Action了。

Java代码

1. 2. 3. 4.

package test.com.app.user; import org.junit.Assert; import org.junit.Test;

import org.springframework.mock.web.MockHttpServletRequest; e;

5. import org.springframework.mock.web.MockHttpServletRespons6. import org.springframework.web.servlet.ModelAndView;

7.

8. import test.JUnitActionBase; 9. 10. /**

11. * 说明: 测试OrderAction的例子 12. *

13. * @author 赵磊

14. * @version 创建时间:2011-2-2 下午10:26:55 15. */ 16.

17. public class TestOrderAction extends JUnitActionBase { 18. @Test

19. public void testAdd() throws Exception {

20. MockHttpServletRequest request = new MockHttpServletRequ

est();

21. MockHttpServletResponse response = new MockHttpServl

etResponse();

22. request.setServletPath(\); 23. request.addParameter(\, \);

24. request.addParameter(\, \); 25. request.setMethod(\); 26. // 执行URI对应的action

27. final ModelAndView mav = this.excuteAction(reques

t, response);

28. // Assert logic

29. Assert.assertEquals(\, mav.getViewName

());

30. String msg=(String)request.getAttribute(\); 31. System.out.println(msg); 32. } 33. }

需要说明一下 :由于当前最想版本的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

十四、转发与重定向


Spring MVC 教程,快速入门,深入分析(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:为客排扰解难

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: