当前位置: 代码迷 >> JavaScript >> Spring mvc 回来JSON
  详细解决方案

Spring mvc 回来JSON

热度:258   发布时间:2013-03-22 09:49:50.0
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){
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
  相关解决方案