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

  • 相关阅读:
    【9018:2221】[伪模板]可持久化线段树
    【9018:2208】可持久化线段树2
    【9018:2207】可持久化线段树1
    【POJ2187】Beauty Contest
    2017/11/22模拟赛
    2017/11/3模拟赛
    [AtCoder 2702]Fountain Walk
    [AtCoder3856]Ice Rink Game
    20170910模拟赛
    20170906模拟赛
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/8509074.html
Copyright © 2011-2022 走看看