首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

深入了解Spring MVC 3(三)

2013-08-04 
深入理解Spring MVC 3(三)package testimport javax.servlet.http.HttpServletRequestimport javax.serv

深入理解Spring MVC 3(三)
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了。

  1. package?test.com.app.user;??
  2. import?org.junit.Assert;??
  3. import?org.junit.Test;??
  4. import?org.springframework.mock.web.MockHttpServletRequest;??
  5. import?org.springframework.mock.web.MockHttpServletResponse;??
  6. 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?MockHttpServletRequest();??
  21. ????????MockHttpServletResponse?response?=?new?MockHttpServletResponse();??
  22. ????????request.setRequestURI("/order/add");??
  23. ????????request.addParameter("id",?"1002");??
  24. ????????request.addParameter("date",?"2010-12-30");??
  25. ????????request.setMethod("POST");??
  26. ????????//?执行URI对应的action??
  27. ????????final?ModelAndView?mav?=?this.excuteAction(request,?response);??
  28. ????????//?Assert?logic??
  29. ????????Assert.assertEquals("order/add",?mav.getViewName());??
  30. ????????String?msg=(String)request.getAttribute("msg");??
  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

?

十四、转发与重定向

可以通过redirect/forward:url方式转到另一个Action进行连续的处理。

可以通过redirect:url 防止表单重复提交?。

写法如下:

return "forward:/order/add";

return "redirect:/index.jsp";

?

?

?

?

?十五、处理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注解

/**  * 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;  }  

?

更多电子书下载

热点排行