Multiple submit buttons in Struts 2 form tag

为君一笑 提交于 2021-02-13 12:41:47

问题


I'm trying to point a button in my form tag to a different action/action class than the form but it won't work. I read in another thread that this is due to a bug in Struts 2 and that struts.mapper.action.prefix.enabled"="true" should be set, so I did it but it's still the same.

I can use a different action pointing to a different method of the same action class that the form is using but when I try specifying a different action class it doesn't work.

This works,

(jsp)

<s:form action="print">     
    <s:iterator value="itemList">
        <s:radio theme="simple" name="item" list="#{id:name}" />
    </s:iterator>

    <div id="functionButtons">
        <s:submit key="button.submit" />
        <s:submit action="cancel" key="button.cancel"/>
    </div>

</s:form>

(struts.xml)

<constant name="struts.mapper.action.prefix.enabled" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />

                     ...

<action name="print" class="...PrintItem" method="perform" > 
    <result name="success">/successJSP.jsp</result>
</action>

<action name="cancel" class="...PrintItem" method="cancel" > 
    <result name="CANCEL" type="redirectAction">homePage</result>
</action>

(action)

public class PrintItem extends BaseAction {

    @Override
    public String perform() throws Exception {

        doPrintLogic();
        return SUCCESS;

    }

    public String cancel(){

        return "CANCEL";

    }
}

but if I change "cancel" action mapping in struts.xml to

<action name="cancel" class="...CancelFormAction" method="perform" > 
    <result name="CANCEL" type="redirectAction">trnsfr</result>
</action>

it doesn't

Is that normal? Is it possible to map to a different action class from within a form that's already mapped to one?


回答1:


This is normal, because you can map the form to only one action specified by the action attribute of the form tag (Historically in HTML). Submit buttons don't change that mapping, instead they tweak an action mapper to use different action because the prefix is enabled. So, if you need to change this behavior you can alter form mapping dynamically or change the default action mapper, etc. However, it would be a problem if the action instance is already created. Hence, you should use default action mapper. But there's a typo in your post struts.mapper.action.prefix.enabled"="true". The constant that enables action on submit buttons is

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

or if you use struts.properties

struts.mapper.action.prefix.enabled=true


来源:https://stackoverflow.com/questions/27869645/multiple-submit-buttons-in-struts-2-form-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!