以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个ResultType,
首先大家先看下Struts2中的源码
包com.opensymphony.xwork2下的DefaultActionInvocation
/**
* 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 自定义标签实例,可以用这个对项目中的实现自定义标签。从而减少代码。
Struts2自定义分页标签很详细了,新手可以少走弯路,节约时间 资源包括了后台如何操作。
Struts2自定义标签,附带配置文件及后台代码,从工作当中整理
Struts2中实现自定义标签很简单,主要分为3步: 1.创建taglib文件(.tld),编写标签声明。 2.编写自定义标签类。 3.在页面中使用标签。 下面以一个自定义的分页标签为例,进行说明。 其实,开发自定义标签并不...
struts2自定义标签struts2自定义标签struts2自定义标签struts2自定义标签
struts实现自定义标签
该资源是使用struts2框架进行操作,返回json格式数据,前台通过ajax提交内容。
tag struts2 自定义标签 实例 tag struts2的自定义标签实例
NULL 博文链接:https://2929582988.iteye.com/blog/2341346
struts2-json-plugin,Struts JSON插件
struts2向服务器返回json数据的一个简单Demo,便于理解
基于struts2的自定义分页标签实现的一个demo,连接数据库为Oracle
struts2自定义校验器实现,希望对初学者有所帮助。
Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON Struts2 Jquery JSON
Struts2自定义类型转换
如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选择。... Struts2返回JSON有两种方式:1.使用Servlet的输出流写入JSON字符串;2.使用Struts2对JSON的扩展。
使用Struts2自带的json包返回json数据