Spring MVC 中JSON响应的处理

@RequestMapping(params = "method=buildtemplate",method = RequestMethod.GET)
public ModelAndView listFormField(String funcId, int fmtId){
        ModelAndView mav = new ModelAndView("/ws/listformfields");
        List<SysFuncformapcolumn> fmcs = this.formManagerService.getColumnInfo(fmtId);    //读取相应的数据库映射信息
        Gson gson=new Gson();
        String json_txt = gson.toJson(fmcs);
        mav.addObject("json_data", json_txt);
        return mav;
    }

能实现目标但很麻烦,查了查网络,原来有简洁的写法:)
@ResponseBody        //用了此注释就不需要单独再建立一个jsp文件作为ajax调用的模板了:)
@RequestMapping(params = "method=buildtemplate",method = RequestMethod.GET)
public String listFormField(String funcId, int fmtId){
        List<SysFuncformapcolumn> fmcs = this.formManagerService.getColumnInfo(fmtId);
        Gson gson=new Gson();
        String json_txt = gson.toJson(fmcs);
        return json_txt;
    }

这样的确很方便,省去了创建一个jsp文件。很简洁

posted @ 2012-02-21 09:05  张良  阅读(798)  评论(0编辑  收藏  举报