zoukankan      html  css  js  c++  java
  • 扩展struts2的结果集StrutsResultSupport 自定义Result处理JSON

    以前在采用Struts2开发的项目中,对JSON的处理一直都在Action里处理的,在Action中直接Response,最近研读了一下Struts2的源码,发现了一个更加优雅的解决办法,自己定义一个ResultType,

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

    包com.opensymphony.xwork2下的DefaultActionInvocation

    472行

    1. /** 
    2.  * Save the result to be used later. 
    3.  * @param actionConfig current ActionConfig 
    4.  * @param methodResult the result of the action. 
    5.  * @return the result code to process. 
    6.  */  
    7. protected String saveResult(ActionConfig actionConfig, Object methodResult) {  
    8.     if (methodResult instanceof Result) {  
    9.         this.explicitResult = (Result) methodResult;  
    10.   
    11.         // Wire the result automatically  
    12.         container.inject(explicitResult);  
    13.         return null;  
    14.     } else {  
    15.         return (String) methodResult;  
    16.     }  
    17. }  


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

    1. this.explicitResult = (Result) methodResult;  
    2.   
    3.             // Wire the result automatically  
    4.             container.inject(explicitResult);  
    5.             return null;  

    现在我们来定义一个接口(JsonResult)来处理一般的POJO对象

    1. package com.kiloway.struts;  
    2.   
    3. import java.io.PrintWriter;  
    4.   
    5. import javax.servlet.http.HttpServletResponse;  
    6.   
    7. import net.sf.json.JSONObject;  
    8. import net.sf.json.JsonConfig;  
    9.   
    10. import org.apache.struts2.ServletActionContext;  
    11. import org.apache.struts2.dispatcher.StrutsResultSupport;  
    12.   
    13. import com.opensymphony.xwork2.ActionInvocation;  
    14.   
    15. public class JsonResult extends StrutsResultSupport {  
    16.   
    17.     private Object result;  
    18.     private JsonConfig jsonConfig;  
    19.   
    20.     public Object getResult() {  
    21.         return result;  
    22.     }  
    23.   
    24.     public JsonResult(JsonConfig jsonConfig) {  
    25.         super();  
    26.         this.jsonConfig = jsonConfig;  
    27.     }  
    28.   
    29.     public void setResult(Object result) {  
    30.         this.result = result;  
    31.     }  
    32.   
    33.     private static final long serialVersionUID = 7978145882434289002L;  
    34.   
    35.     @Override  
    36.     protected void doExecute(String finalLocation, ActionInvocation invocation)  
    37.             throws Exception {  
    38.         HttpServletResponse response = null;  
    39.         try {  
    40.             response = ServletActionContext.getResponse();  
    41.             PrintWriter printWriter = response.getWriter();  
    42.             if (jsonConfig != null) {  
    43.                 printWriter.write(JSONObject.fromObject(result).toString());  
    44.             } else {  
    45.                 printWriter.write(JSONObject.fromObject(result, jsonConfig)  
    46.                         .toString());  
    47.             }  
    48.         }catch(Exception e){  
    49.             throw new Exception("json parse error!");  
    50.         } finally {  
    51.             response.getWriter().close();  
    52.         }  
    53.   
    54.           
    55.   
    56.     }  
    57. }  


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

    我们在struts.xml里面可以这样定义

    1. <package name="default" namespace="/" extends="struts-default">  
    2.         <result-types>  
    3.             <result-type name="jsonResult" class="com.kiloway.struts.JsonResult"/>  
    4.         </result-types>  
    5.   
    6.         <action name="student" class="com.kiloway.struts.Student">  
    7.             <result name="json" type="jsonResult"/>  
    8.         </action>  
    9.     </package>  


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


    Action 中直接这样调用

    1. public JsonResult getJson()  
    2.     {  
    3.         UserInfo f = new UserInfo();  
    4.         f.setName("小睿睿");  
    5.         f.setPassword("哈哈");  
    6.         JsonResult jsonResult  = new JsonResult();  
    7.         jsonResult.setResult(f);  
    8.         return jsonResult;  
    9.     }  



    在我们的Action代码中就不用response.write了,完全交给了Reuslt对象去处理了(doExecute)

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




    在我下载的最新的struts的开发包里,发现了一个JSON处理插件 struts2-json-plugin-2.3.8.jar

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

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

  • 相关阅读:
    Linux C/C++编程之(十四)文件操作相关函数
    javascript语法之循环语句
    javascript语法之流程控制语句
    javascript语法之字符串转换成数字
    javascript语法之声明变量
    认识javascript
    css之定位
    css之盒子模型案例
    常见Css样式
    Css详解之(伪类选择器)
  • 原文地址:https://www.cnblogs.com/langtianya/p/5052148.html
Copyright © 2011-2022 走看看