Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top