zoukankan      html  css  js  c++  java
  • 扩展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

  • 相关阅读:
    Android Studio无法预览xml布局之解决方法(两种)
    ssm web.xml配置解析
    ssm框架下实现文件上传
    spring mvc使用@InitBinder 标签对表单数据绑定
    Jquery实现相对浏览器位置固定、悬浮
    asp,php,jsp 不缓存网页的办法
    Spring 2.5
    ERROR 1366 (HY000): Incorrect string value: 'xB3xA4xC9xB3' for column
    DELPHI SOKET 编程--使用TServerSocket和TClientSocket
    SVN switch 用法总结
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/8509074.html
Copyright © 2011-2022 走看看