扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON

 

 

首先大家先看下Struts2中的源码

472行

  •                 
  •                  container.inject(explicitResult);  
  •              }               }  
  • }  

如果resultType实现了Result接口,则执行

  •   
  •                          container.inject(explicitResult);  
  •             现在我们来定义一个接口(JsonResult)来处理一般的POJO对象

     

  •   
  •   
  •   
  •   
  • import org.apache.struts2.ServletActionContext;  
  •   
  •   
  •   
  •             
  •              }  
  •   
  •                       }  
  •                   }  
  •        
  •                                HttpServletResponse response =                       response = ServletActionContext.getResponse();  
  •             PrintWriter printWriter = response.getWriter();  
  •                 printWriter.write(JSONObject.fromObject(result).toString());  
  •                 printWriter.write(JSONObject.fromObject(result, jsonConfig)  
  •             }  
  •             
  •             response.getWriter().close();  
  •   
  •   
  • }  

JsonReulst定义好了该如何让Struts处理呢?

 

"default" "/" "struts-default"         >  
  •             "jsonResult" "com.kiloway.struts.JsonResult"         >  
  •   
  • "student" "com.kiloway.struts.Student"             "json" "jsonResult"         >  
  •     >  

reuslt的name可以任意,但type必须和你注册的ResultType相同。

 

 

    {  
  •         UserInfo f =          f.setName(         f.setPassword(         JsonResult jsonResult  =          jsonResult.setResult(f);  
  •     }  

 

 

这样就很方便的处理了JSON格式的数据

 

 

 

该插件提供了更完善的JSON处理解决方案,下篇文章会介绍该插件的使用方式

来源:http://blog.csdn.net/myxx520/article/details/8655088

转载于:https://www.cnblogs.com/lvdongjie/p/8509074.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值