扩展struts2的结果集StrutsResultSupport 自定义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的结果集StrutsResultSupport 自定义Result处理JSON的更多相关文章
- Struts2自定义Result处理JSON
以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个 ...
- JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- jquery序列化from表单使用ajax提交返回json数据(使用struts2注解result type = json)
1.action类引入struts2的"json-default"拦截器栈 @ParentPackage("json-default") //示例 @Paren ...
- 【bzoj4869】[Shoi2017]相逢是问候 扩展欧拉定理+并查集+树状数组
题目描述 Informatik verbindet dich und mich. 信息将你我连结. B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以分为两种:0 ...
- 制作ACK集群自定义节点镜像的正确姿势
随着云原生时代的到来,用户应用.业务上云的需求也越来越多,不同的业务场景对容器平台的需求也不尽相同,其中一个非常重要的需求就是使用自定义镜像创建ACK集群. ACK支持用户使用自定义镜像创建Kuber ...
- Struts2 中 result type=”json” 的参数解释
转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...
- springmvc 自定义view支持json和jsonp格式数据返回
1.如果controlloer上用@ResponseBody注解,则用<mvc:message-converter>里面配置的json解析器进行解析 <mvc:annotation- ...
- 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 ...
- Loadrunner请求自定义的http(json)文件and参数化
Loadrunner请求自定义的http(json)文件and参数化 研究啦好些天这个东西啦 终于出来答案啦 嘿嘿 给大家分享一下 : 请求自定义的http文件用函数:web_custom_ ...
随机推荐
- 【HDU 5818多校】Joint Stacks
用两个栈模拟,并保存每个点的时间戳.每次合并的时候记录合并时的时间戳mcnt和此时的topa和topb记做ta.tb. 每次pop的时候,如果栈的top的时间戳大于mcnt,则普通地pop,否则就在两 ...
- php验证登录
<html><head> <title></title> <meta charset="utf-8"></head ...
- Redis哈希-hash
Redis的hash类型数据存储极为重要 hset K V 赋值一个hash 其中V为 (key, value) 127.0.0.1:6379> hset user id 1(integer) ...
- springMVC-自定义数据类型转换器
自定义类型转换器 201603005,今天想了一个问题,Spring中的Conventer是如何实现的,因为他没有绑定类中的属性,它怎么知道要将那个String转换?看了几遍的书也没有找到,后来想想, ...
- 先装.net后装iis的问题
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:(即就是先装的是visual stuido 2010的话,在安装IIS 7) 32位的Windows:----------- ...
- Android成长日记-Activity
① Activity是一个应用程序组件,提供用户与程序交互的界面 ② Android四大组件 ---Activity ---Service ---BroadcastReceiver ---Conten ...
- ecshop /goods.php SQL Injection Vul
catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: poc http://localhost ...
- IEnumerable 遍历用法
咋一看到IEnumerable这个接口,我们可能会觉得很神奇,在一般的编程时,基本上我们是想不到去用它的,可是,俗话说得好,存在便是道理,那么,它对我们来说,能够带来哪些奇妙的事情呢? 要想弄懂它,我 ...
- ubantu eclipe
sudo tar zxvf '/tmp/eclipse-inst-linux64.tar.gz' -C/usr/lib 4.在终端输入: $ sudo gedit /usr/share/applica ...
- UIViewController 的 presentedViewController 和 presentingViewController
#import "TestViewController.h" #import "OneViewController.h" 在TextViewController ...