当前位置: 代码迷 >> Ajax >> 扩充Spring MVC 用以支持Controller对Ajax的处理
  详细解决方案

扩充Spring MVC 用以支持Controller对Ajax的处理

热度:898   发布时间:2012-08-22 09:50:35.0
扩展Spring MVC 用以支持Controller对Ajax的处理

Spring Mvc中没有特别针对于Ajax请求的处理Controller.但是AbstractController和?
SimpleFormController也是可以针对Ajax的请求给与相应的.但显得有一点点的不自然.?

Java代码??收藏代码
  1. @Override??
  2. protected?ModelAndView?onSubmit(HttpServletRequest?request,?HttpServletResponse?response,?Object?cmd,??
  3. ????????BindException?ex)?{??
  4. ????String?userId=request.getParamater("userId");??
  5. ?????????????????Map?userInfoMap=userDao.getUserBaseInfo(userId);??
  6. ?????????????????JSONObject?object?=?new?JSONObject();??
  7. ?????????????????object.put("userInfoMap",userInfoMap);??
  8. ????response.setContentType("text/Xml;charset=gbk");??
  9. ????PrintWriter?out?=?null;??
  10. ????try?{??
  11. ????????out?=?response.getWriter();??
  12. ?????????????out.println(object.toString());??
  13. ???????????????
  14. ????}??
  15. ????catch?(IOException?ex1)?{??
  16. ????????ex1.printStackTrace();??
  17. ????}finally{??
  18. ???????????????????????????out.close();??
  19. ??????????????????}??
  20. ????return?null;??
  21. }??

说明一下我向客户端页面response的是一个JSon对象,选择JSon这种数据格式的两个原因是:?

  1. 体积小可降低流量.
  2. Js端解码十分方便,Js对JSon格式数据有原生的支持.

使用Spring Mvc原有的SimpleFormController,从上边的例子上看是可以实现的,但是每次都要写"将JSonObject写入流,关流的代码.很累,很麻纺."?
所以,扩展了两个新的Controller,以简化对Ajax请求的处理.?
JSONObjectAbstractController 用于简单的从request.getParamater参数的情况.?
JSONObjectAbstractController 的原代码:?
Java代码??收藏代码
  1. package?com.javaest.ext.spring.mvc.controller;??
  2. ??
  3. import?java.io.IOException;??
  4. import?java.io.PrintWriter;??
  5. ??
  6. import?javax.servlet.http.HttpServletRequest;??
  7. import?javax.servlet.http.HttpServletResponse;??
  8. ??
  9. import?net.sf.json.JSONObject;??
  10. ??
  11. import?org.springframework.web.servlet.ModelAndView;??
  12. import?org.springframework.web.servlet.mvc.AbstractController;??
  13. ??
  14. public?abstract?class?JSONObjectAbstractController?extends?AbstractController?{??
  15. ??
  16. ????@Override??
  17. ????protected?ModelAndView?handleRequestInternal(HttpServletRequest?request,?HttpServletResponse?response)??
  18. ????????????throws?Exception?{??
  19. ????????JSONObject?jsonObject?=?handleAjaxRequestInternal(request,?response);??
  20. ????????response.setContentType("text/Xml;charset=gbk");??
  21. ????????PrintWriter?out?=?null;??
  22. ????????try?{??
  23. ????????????out?=?response.getWriter();??
  24. ????????????out.println(jsonObject.toString());??
  25. ????????}??
  26. ????????catch?(IOException?ex1)?{??
  27. ????????????ex1.printStackTrace();??
  28. ????????}??
  29. ????????finally?{??
  30. ????????????out.close();??
  31. ????????}??
  32. ??
  33. ????????return?null;??
  34. ????}??
  35. ??
  36. ????protected?abstract?JSONObject?handleAjaxRequestInternal(HttpServletRequest?httpServletRequest,??
  37. ????????????HttpServletResponse?httpServletResponse)?throws?Exception;??
  38. ??
  39. }??

JSONObjectSimpleFormController 用于自动将请求参数绑定到Command对象的情况.?
JSONObjectSimpleFormController 的代码:?
Java代码??收藏代码
  1. package?com.javaest.ext.spring.mvc.controller;??
  2. ??
  3. import?java.io.IOException;??
  4. import?java.io.PrintWriter;??
  5. ??
  6. import?javax.servlet.http.HttpServletRequest;??
  7. import?javax.servlet.http.HttpServletResponse;??
  8. ??
  9. import?net.sf.json.JSONObject;??
  10. ??
  11. import?org.springframework.validation.BindException;??
  12. import?org.springframework.web.servlet.ModelAndView;??
  13. import?org.springframework.web.servlet.mvc.SimpleFormController;??
  14. ??
  15. public?abstract?class?JSONObjectSimpleFormController?extends?SimpleFormController?{??
  16. ??
  17. ????@Override??
  18. ????protected?ModelAndView?onSubmit(HttpServletRequest?request,?HttpServletResponse?response,?Object?cmd,??
  19. ????????????BindException?ex)?{??
  20. ????????JSONObject?object?=?onAjaxSubmit(request,?response,?cmd,?ex);??
  21. ??
  22. ????????response.setContentType("text/Xml;charset=gbk");??
  23. ????????PrintWriter?out?=?null;??
  24. ????????try?{??
  25. ????????????out?=?response.getWriter();??
  26. ????????????out.println(object.toString());??
  27. ????????}??
  28. ????????catch?(IOException?ex1)?{??
  29. ????????????ex1.printStackTrace();??
  30. ????????}??
  31. ????????finally?{??
  32. ????????????out.close();??
  33. ????????}??
  34. ??
  35. ????????return?null;??
  36. ????}??
  37. ??
  38. ????public?abstract?JSONObject?onAjaxSubmit(HttpServletRequest?request,?HttpServletResponse?response,?Object?cmd,??
  39. ????????????BindException?ex);??
  40. ??
  41. }??



以下就是我为了支持Ajax请求的处理所扩展的SpringMvc Controller.?

注:JSONObject?是json-lib-2.3-jdk15.jar中的,请自己下载导入
原帖:
http://javaest.iteye.com/blog/187504

  相关解决方案