如何在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>
参见文档了解更多详情