Struts2-JSON plugin working?

216 views Asked by At

I know that Struts2 JSON Plugin converts whole Action class into JSON form and by supplying a root param we can let it convert only one param into JSON form.

What if I want out of 5 members of my action class, only 2 or 3 of my members to be converted to JSON? Is this possible?

3

There are 3 answers

1
coding_idiot On BEST ANSWER

You can use includeProperties or excludeProperties.

IncludeProperties didn't worked as expected in my case & hence, I prefer using exludeProperties instead.

The action configuration will look like

<action name="camp/ls" class="actions.MyAction">
            <result type="json">
                <param name="excludeProperties">
                    dataIsp,deviceBlacklist,deviceOs,deviceWhitelist,exchanges
                </param>
            </result>
        </action>
0
Roman C On

In this case you should not use root parameter but use includeProperties parameter instead. See this link for reference documentation.

A comma-delimited list of regular expressions can be passed to the JSON Result to restrict which properties will be serialized. ONLY properties matching any of these regular expressions will be included in > the serialized output.

Note:

Exclude property expressions take precedence over include property expressions. That is, if you use include and exclude property expressions on the same result, include property expressions will not be applied if an exclude exclude property expression matches a property first.

<!-- Result fragment -->
<result type="json">
  <param name="includeProperties">
    ^entries\[\d+\]\.clientNumber,
    ^entries\[\d+\]\.scheduleNumber,
    ^entries\[\d+\]\.createUserId
  </param>

</result>
0
Andrea Ligios On

The easiest way is to remove the getters from the properties you don't want to be JSONized.

Only (non-transient) properties with a getter will.