Lv.的博客

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

posted @   Avatarx  阅读(309)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2017-03-05 [Go语言]cgo用法演示
2017-03-05 Windows环境下的安装gcc
点击右上角即可分享
微信分享提示