Struts2自定义Result处理JSON
以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个ResultType,
首先大家先看下Struts2中的源码
包com.opensymphony.xwork2下的DefaultActionInvocation
472行
/**
* Save the result to be used later.
* @param actionConfig current ActionConfig
* @param methodResult the result of the action.
* @return the result code to process.
*/
protected String saveResult(ActionConfig actionConfig, Object methodResult) {
if (methodResult instanceof Result) {
this.explicitResult = (Result) methodResult; // Wire the result automatically
container.inject(explicitResult);
return null;
} else {
return (String) methodResult;
}
}
如果resultType实现了Result接口,则执行
this.explicitResult = (Result) methodResult; // Wire the result automatically
container.inject(explicitResult);
return null;
现在我们来定义一个接口(JsonResult)来处理一般的POJO对象
package com.kiloway.struts; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject;
import net.sf.json.JsonConfig; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport; import com.opensymphony.xwork2.ActionInvocation; public class JsonResult extends StrutsResultSupport { private Object result;
private JsonConfig jsonConfig; public Object getResult() {
return result;
} public JsonResult(JsonConfig jsonConfig) {
super();
this.jsonConfig = jsonConfig;
} public void setResult(Object result) {
this.result = result;
} private static final long serialVersionUID = 7978145882434289002L; @Override
protected void doExecute(String finalLocation, ActionInvocation invocation)
throws Exception {
HttpServletResponse response = null;
try {
response = ServletActionContext.getResponse();
PrintWriter printWriter = response.getWriter();
if (jsonConfig != null) {
printWriter.write(JSONObject.fromObject(result).toString());
} else {
printWriter.write(JSONObject.fromObject(result, jsonConfig)
.toString());
}
}catch(Exception e){
throw new Exception("json parse error!");
} finally {
response.getWriter().close();
}
}
}
JsonReulst定义好了该如何让Struts处理呢?
我们在struts.xml里面可以这样定义
<package name="default" namespace="/" extends="struts-default">
<result-types>
<result-type name="jsonResult" class="com.kiloway.struts.JsonResult"/>
</result-types> <action name="student" class="com.kiloway.struts.Student">
<result name="json" type="jsonResult"/>
</action>
</package>
reuslt的name可以任意,但type必须和你注册的ResultType相同。
Action 中直接这样调用
public JsonResult getJson()
{
UserInfo f = new UserInfo();
f.setName("小睿睿");
f.setPassword("哈哈");
JsonResult jsonResult = new JsonResult();
jsonResult.setResult(f);
return jsonResult;
}
在我们的Action代码中就不用response.write了,完全交给了Reuslt对象去处理了(doExecute)
这样就很方便的处理了JSON格式的数据
struts的开发包里,发现了一个JSON处理插件 struts2-json-plugin-2.3.8.jar
该插件提供了更完善的JSON处理解决方案。
原文http://blog.csdn.net/myxx520/article/details/8655088
Struts2自定义Result处理JSON的更多相关文章
- 扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON
以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个 ...
- jquery序列化from表单使用ajax提交返回json数据(使用struts2注解result type = json)
1.action类引入struts2的"json-default"拦截器栈 @ParentPackage("json-default") //示例 @Paren ...
- Struts2 中 result type=”json” 的参数解释
转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...
- Struts2 自定义Result
注意:我只要是解决自定义返回Json 和异常处理问题 新建一个类 AjaxResult 继承 StrutsResultSupport 看看代码吧 public class AjaxResult e ...
- Struts2自定义返回Json类型result
本来Struts2有自己的json类型的返回结果,并提供了插件,但是它有一个问题,那就是它会将所有序列化的字段都返回,如果想要制定返回Action的某一个属性,则需要在配置result时,配置参数(这 ...
- Struts2 配置文件result的name属性和type属性
Struts2 配置文件result的name属性和type属性:Name属性SUCCESS:Action正确的执行完成,返回相应的视图,success是 name属性的默认值: NONE:表示Act ...
- Struts2配置RESULT中TYPE的参数说明
chain 用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony.xwork2.Acti ...
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- Caused by: The Result type [json] which is defined in the Result annotation on the class
1.错误描述 严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] at co ...
随机推荐
- hive学习(六) 参数和动态分区
1.hive 参数.变量 1.1hive的命名空间: hive当中的参数.变量,都是以命名空间开头
- 【LOJ】#2017. 「SCOI2016」围棋
题解 考虑到状态数比较复杂,其实我们需要轮廓线dp-- 我们设置\(f[x][y][S][h][k]\)为考虑到第(x,y)个格子,S是轮廓线上的匹配状态,是二进制,如果一位是1表示这一位匹配第一行匹 ...
- 【JAVAWEB学习笔记】网上商城实战1:环境搭建和完成用户模块
今日任务 完成用户模块的功能 1.1 网上商城的实战: 1.1.1 演示网上商城的功能: 1.1.2 制作目的: 灵活运用所学知识完成商城实战. 1.1.3 数据库分析和设 ...
- FastReport.Net使用:[8]交叉表一
1.绘制报表标题,交叉表可以直接放在标题栏内. 2.拖动一交叉表控件到标题栏内. 3.设置交叉表的行列信息. 将Tabel中的[科室名称]列拖到交叉表的列上以创建列,将Tabel中的[姓名]列拖到交叉 ...
- 【BZOJ 3106】 3106: [cqoi2013]棋盘游戏 (对抗搜索)
3106: [cqoi2013]棋盘游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 544 Solved: 233 Description 一个 ...
- hdu 3089 (快速约瑟夫环)
Josephus again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【递推】hdu5927 Auxiliary Set
题意:给你一棵树.q次询问,每次给你一些非关键点,其他的点都是关键点,让你输出树中既不是关键点,也不是关键点的lca的点的数量. 对每次询问的非关键点按照深度从深到浅排序,依次处理,最开始每个点受到的 ...
- 【lct】bzoj1036 [ZJOI2008]树的统计Count
题意:给你一棵树,点带权,支持三种操作:单点修改:询问链上和:询问链上max. 这里的Query操作用了与上一题不太一样的做法(上一题用那种做法,因为在边带权的情况下换根太困难啦): 先ChangeR ...
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
- Codeforces Round #346 (Div. 2) B. Qualifying Contest 水题
B. Qualifying Contest 题目连接: http://www.codeforces.com/contest/659/problem/B Description Very soon Be ...