您的位置:首页 > 编程语言 > Java开发

struts2 返回json数据

2016-03-30 17:14 423 查看
action 的方法 

public String loadGroupShopList(){
Map<String,Object> map=new HashMap<String, Object>();
HttpServletResponse httpresponse = ServletActionContext.getResponse();
httpresponse.setContentType("text/plain;charset=UTF-8");
PrintWriter writer = null;
List<GroupShopDTO> list=null;

try {
writer = httpresponse.getWriter();
} catch (IOException ex) {
ex.printStackTrace();
logger.error("call Response.getWriter() failed", ex);
}
try {

code=200;
if (activityId == null) {
code = 401;
msg = "参数为空";
}
list=groupShopService.loadGroupShoupByActivityId(activityId);
if(list ==null || list.size()<1){
code = 300;
msg = "没有数据";
}
map.put("code",String.valueOf(code));
map.put("msg",msg);
map.put("data",list);
String json=JsonUtils.toJson(map);
writer.println(json);
writer.close();
}

catch (Exception e){
e.printStackTrace();
code=500;
msg="系统错误!";
}

return null;
}

JsonUtils这个工具类 我文章里面有 需要的可以在我的博客里面看看 源码 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: