Struts 2 JSON plugin and wildcard issue

264 views Asked by At

In my struts.xml a convention is followed to call actions for a particular action class like this:

struts.xml:

<package name="cdot.oss.cmsat.gma.struts" extends="struts-default" namespace="/">
    <action name="*ConfigureTspThreshold"
                class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="{1}">
                <result name="display">pages/ConfigureTspThresholdInput.jsp</result>
    </action>
</package>

I get method name through wildcard and ConfigureTspThresholdAction is the class name.

I am using struts2-json-plugin to convert data to JSON. Now, for some actions I want to return JSON data using Struts2 JSON plugin.

So I need to use extends json-default and result-type json for some actions like this:

<action name="*ConfigureTspThreshold" class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction" method="{1}">               
    <result type="json">
        <param name="excludeProperties">
            tspNameIdMap
        </param>
    </result>
</action>

<action name="*ConfigureTspThreshold" class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction"
            method="{1}">
    <result type="json">
        <param name="excludeProperties">
        thresholdParameters
        </param>
    </result>
</action>

So different exclude-properties with same result-type json .

How to fit these JSON result types in the convention followed?

As the last two actions will conflict as they have same result type json ?

2

There are 2 answers

0
Roman C On

One of the option is to use dynamic parameters in the result configuration. You can always modify the result in the action before result is executed. Look at this answer.

You could use a dynamic parameters with a result, see the dynamic result configuration.

In the action you should write a getter for the patrameter

private String actionUrl;

public String getActionUrl() {
   return actionUrl;
}

and configure result

<action name="create" class="CreateAction">
   <result type="redirect">${actionUrl}</result>
</action>
0
Sumit On

What I would do is create a property in your action class myexcludedProperties

And then in your action methods, set this myexcludedProperties

and then change your action mapping to

<action name="*ConfigureTspThreshold" class="cdot.oss.cmsat.gma.struts.ConfigureTspThresholdAction"
            method="{1}">
            <result type="json">
                <param name="excludeProperties">
                    ${myexcludedProperties}
                </param>
            </result>
</action>

I believe ${myexcludedProperties} is the correct syntax.

If you don't want to code a property, you have to figure out how to differentiate requests (method="{1}") so that you can set the correct param.

Hope that makes sense.