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这个工具类 我文章里面有 需要的可以在我的博客里面看看 源码
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这个工具类 我文章里面有 需要的可以在我的博客里面看看 源码
相关文章推荐
- Struts2国际化
- Struts2标签
- [Java]XML数据的请求和DOM技术解析
- java中可变长参数的定义及使用方法
- SpringMVC 重定向后台传参数 中文乱码问题
- 为Struts2指定多个配置文件
- Struts2文件上传
- Struts2文件上传和下载
- Struts2常用属性配置
- Struts2视图类型
- MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3
- 经典冒泡排序算法JAVA版本
- Java System
- spring-data-jpa初步开始的helloworld
- Struts2 中 OGNL各作用域对象怎么取
- springmvc附件上传核心代码
- 基于注解的Spring AOP的配置和使用
- Java解析HTML之HTMLParser使用与详解
- java servlet的工作原理
- servlet---java读取本地文件