Spring2.5 MVC 2.5.6 传JSON对象

网上大部分是spring3.0的传JSON对象,spring3自带了支持JSON对象。

关于spring2.5,也很简单。导入两个jackson的jar包,然后手动传json对象

Java代码 复制代码 收藏代码
  1. jackson-core-asl-1.9.11.jar
  2. jackson-mapper-asl-1.9.11.jar

原来controller里面传ModelAndView:

Java代码 复制代码 收藏代码
  1. public ModelAndView test(HttpServletRequest req, HttpServletResponse res)
  2. throws Exception {
  3. return new ModelAndView(view, model);
  4. }

改成传json对象

Java代码 复制代码 收藏代码
  1. public ModelAndView test(HttpServletRequest req, HttpServletResponse res)
  2. throws Exception {
  3. ObjectMapper mapper = new ObjectMapper();
  4. res.getOutputStream().print(mapper.writeValueAsString(dinnerList));
  5. res.getOutputStream().flush();
  6. res.getOutputStream().close();
  7. // res.getOutputStream().write(new String("No parameter found.").getBytes());
  8. return null;
  9. }

原文地址:https://www.cnblogs.com/bjanzhuo/p/3576102.html