扩展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