Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

java - Multiple submit buttons in Struts 2 form tag

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...