xiaohan2012
xiaohan2012

Reputation: 10342

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

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?

Upvotes: 5

Views: 1514

Answers (3)

xrcwrn
xrcwrn

Reputation: 5327

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

Upvotes: 0

user497087
user497087

Reputation: 1591

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

Upvotes: 0

nmc
nmc

Reputation: 8696

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

Upvotes: 5

Related Questions