1

在我struts.xml的约定中,为特定的动作类调用动作,如下所示:

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>

我通过通配符获取方法名,ConfigureTspThresholdAction是类名。

我正在使用struts2-json-plugin将数据转换为 JSON。现在,对于某些操作,我想使用 Struts2 JSON 插件返回 JSON 数据。

所以我需要对一些这样json-default的操作使用扩展和结果类型json

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

因此具有相同结果类型的不同排除属性json

如何在遵循的约定中适应这些 JSON 结果类型?

最后两个动作会因为它们具有相同的结果类型而发生冲突json

4

2 回答 2

1

我要做的是在您的操作类中创建一个属性myexcludedProperties

然后在你的动作方法中,设置这个myexcludedProperties

然后将您的动作映射更改为

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

我相信${myexcludedProperties}是正确的语法。

如果您不想对属性进行编码,则必须弄清楚如何区分请求(method="{1}"),以便您可以设置正确的参数。

希望这是有道理的。

于 2013-12-09T13:24:01.240 回答
0

一种选择是在结果配置中使用动态参数。您始终可以在执行结果之前修改操作中的结果。看看这个答案。

您可以使用带有结果的动态参数,请参阅动态结果配置。

在操作中,您应该为参数编写一个 getter

private String actionUrl;

public String getActionUrl() {
   return actionUrl;
}

并配置结果

<action name="create" class="CreateAction">
   <result type="redirect">${actionUrl}</result>
</action>
于 2013-12-09T13:12:40.270 回答