网上大部分是spring3.0的传JSON对象,spring3自带了支持JSON对象。
关于spring2.5,也很简单。导入两个jackson的jar包,然后手动传json对象
jackson-core-asl-1.9.11.jar jackson-mapper-asl-1.9.11.jar
原来controller里面传ModelAndView:
public ModelAndView test(HttpServletRequest req, HttpServletResponse res) throws Exception { return new ModelAndView(view, model); }
改成传json对象
public ModelAndView test(HttpServletRequest req, HttpServletResponse res) throws Exception { ObjectMapper mapper = new ObjectMapper(); res.getOutputStream().print(mapper.writeValueAsString(dinnerList)); res.getOutputStream().flush(); res.getOutputStream().close(); // res.getOutputStream().write(new String("No parameter found.").getBytes()); return null; }