日期:2014-05-16  浏览次数:20337 次

Spring mvc 返回JSON

public ModelAndView updateContractTitle (HttpServletRequest request,
??? HttpServletResponse response) throws Exception {
??????? ModelAndView mav = new ModelAndView();
??????? long applyId = Long.valueOf(request.getParameter("applyId"));
??????? AbstractApply apply = applyDao.getApplyById(applyId);
??????? apply.setApplyTitle(request.getParameter("contractTitle"));
??????? for(Object t : apply.getApplyTarget()){
??????????? AbstractApplyTarget c = (AbstractApplyTarget) t;
??????????? c.setTitle(apply.getApplyTitle());
??????? }
??????? applyDao.merge(apply);//以上是业务逻辑

??????? //开始返回
??????? MappingJacksonJsonView view = new MappingJacksonJsonView();
??????? Map attributes = new HashMap();
??????? attributes.put("success", Boolean.TRUE);

?attributes.put("contractNo", "contractNo");

??????? view.setAttributesMap(attributes);
??????? mav.setView(view);
??????? return mav;
??? }

?

?
?
var URL = "approve.do?action=approveC"
? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? type:"POST",
? ? ? ? ? ? ? ? url:URL,
? ? ? ? ? ? ? ? data:null,
? ? ? ? ? ? ? ? success:function(msg){
? ? ? ? ? ? ? ? ? ? alert(msg.contractNo);//取到返回的“contractNo”值
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? error:function(msg){
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });