Transfer only a part of properties in a class in struts' json
Sorry, I really don't know how to summarize the title of this question. So, the title may not be clear.
I have an action class which performs some business logic.
in the Action Class:
class ActionClass extends ActionSupport{
private Merchandise merchandise;// I want to transfer it to the client
//setter and getter
}
in the Merchandise class:
class Merchandise{
private String name; // I want to transfer it
private String price; //I don't want to transfer it
private String description;//I don't want to transfer it
//setter and gett开发者_StackOverflow社区er
}
Now, I need to transfer the merchandise property in ActionClass to the client.
However, in the merchandise property, I want to transfer only the name property while inhibiting the other two properties.
Then how to inhibit the transfer of the other two properties(price and description) in class Merchandise?
Try something like:
<!-- Result fragment -->
<result type="json">
<param name="root">merchandise</param>
<param name="excludeProperties">price,description</param>
</result>
See full documentation, other options and examples at http://struts.apache.org/2.2.3/docs/json-plugin.html
The easiest way is to create a Data Transfer Object in your action class that contains only the fields you want to send to the client and make that your root object
@nmc answer is correct another way you can try like:
<result type="json">
<param name="root">merchandise</param>
<param name="includeProperties">name</param>
</result>
Or
<result type="json">
<param name="includeProperties">
merchandise.name
</param>
<param name="root">
#action
</param>
</result>
精彩评论