Spring2.5 MVC 2.5.6 传JSON对象

网上大部分是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;

	}

猜你喜欢

转载自kavinhub.iteye.com/blog/1779803
今日推荐