简体   繁体   English

Struts 2表单标签中的多个提交按钮

[英]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. 我试图将表单标签中的按钮指向与表单不同的action / action类,但是它不起作用。 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. 我在另一个线程中读到,这是由于Struts 2中的错误所致,应该设置struts.mapper.action.prefix.enabled"="true" ,所以我做到了,但还是一样。

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) (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 ) 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 但是如果我将struts.xml “取消”操作映射更改为

<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? 是否可以从已经映射到一个动作的表单中映射到另一个动作类?

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). 这是正常现象,因为您只能将表单映射到由form标记的action属性指定的一个动作(历史上为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 但是您的文章struts.mapper.action.prefix.enabled"="true"有一个错字。启用对提交按钮的操作的常量是

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

or if you use struts.properties 或者如果您使用struts.properties

struts.mapper.action.prefix.enabled=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM