xiaohan2012
xiaohan2012

Reputation: 10342

How to return json data selectively in a struts2 action class

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.

And my problem is like this:

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: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.

Upvotes: 5

Views: 5288

Answers (1)

nmc
nmc

Reputation: 8696

Struts2-JSON plugin allows you to exclude null properties

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

See documentation for more details

Upvotes: 12

Related Questions