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

  • 相关阅读:
    Django之 Choices参数、MVC和MTV模型、Ajax
    Django之聚合查询、分组查询、查询优化以及F、Q查询
    Django 之 模型层ORM操作
    Django关于视图层和模板层相关操作
    Django数据操作、ORM表设计、路由层
    Django三板斧,Django连接数据库
    前端--jQuery
    前端----BOM和DOM
    .zip.001 -- .zip.003解压缩
    HTTP Status Codes
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/8509074.html
Copyright © 2020-2023  润新知