如何在 struts2 动作类中选择性地返回 json 数据 [英] How to return json data selectively in a struts2 action class

查看:26
本文介绍了如何在 struts2 动作类中选择性地返回 json 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个动作类中有几个带有 getter 和 setter 方法的属性.

I have several properties with getter and setter method in one action class.

这些属性不执行相同的任务.实际上,它们响应不同的业务服务请求,或者它们与不同的操作相关.

Those properties do not perform the same task. Actually, they respond to different business-service requests, or they are related to different actions.

而我的问题是这样的:

我需要过滤掉数据并仅返回属性集中的部分属性,因为在单个请求(操作)中并非所有属性都是必需的.

I need to filter out the data and return only part of the properties within the property set because not all properties are necessary in a single request(action).

PS:实际上,我可能已经将这些动作或业务逻辑分成几个类,而不是把它们放在一个动作类中.但是,我认为它们都共享相似的 DAO 和服务,因此我将它们放在一起以防止冗余 IOC.

PS:Actually, I might have separated those actions or business logic into several classes instead of putting them into one action class. However, I think they all share the similar DAOs and services so I put them together in order to prevent redundant IOCs.

推荐答案

Struts2-JSON 插件 允许您排除空属性

<result type="json">
  <param name="excludeNullProperties">true</param>
</result>

或排除某些参数被序列化

or exclude certain parameters from being serialized

<result type="json">
  <param name="excludeProperties">
    login.password,
    studentList.*.sin
  </param>
</result>

有关详细信息,请参阅文档

这篇关于如何在 struts2 动作类中选择性地返回 json 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆