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 ...
随机推荐
- NSBundle pathForResource is NULL 取不到值
错误提示: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL i ...
- python基础(10)--模块
模块(module) 模块,用一坨代码实现了某个功能的代码集合 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来 ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
- vue配置二级目录&vue-axios跨域办法&谷歌浏览器设置跨域
一.根据官方建议,dist打包的项目文件放在服务器根目录下,但是很多时候,我们并不能这样做,当涉及到二级目录设置多层深埋的时候,就需要在webpack配置文件里去设置一下了. 在webpack.con ...
- lr11_Vugen_Genrial Options选项介绍:
lr11_Vugen_Genrial Options选项介绍:
- Java Integer Cache
Java Integer Cache Java 代码 public class IntegerDemo { public static void main(String[] args) { Integ ...
- phpstorm配置关联php手册
最近发现有些编辑器可以 选中函数名,通过相应的快捷键就可以调用 浏览器 打开相应 函数的 在线帮助文档. 一番查找,我终于发现 phpStorm 也有相应的功能. 一.自带功能 在编辑器中选中函数名, ...
- SSM相关资料
MyBatis: 官方文档 MyBatis-Spring:官方文档 SpringMVC整合MyBatis实例 SSM框架详细整合教程 一步一步带你搭建后台管理系统之SSM框架整合 SSM框架入门和搭建 ...
- c++源文件后缀名问题
VC里用cpp作后缀名, 在GCC里默认采用C.cc.cxx作为后缀名 .cpp, .h (VS file).cc, .h (GCC file) C中: 头文件后缀名: .h 源文件后缀名: .c ...
- php基础知识一
1.PHP是什么: 开源,免费的,跨平台的 2.PHP能做什么: 3.PHP的特点: 4.PHP的标记风格: <?php ?> <? ?> <script languag ...