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

2022-01-16 00:00:00 action forms java jsp struts2

我试图将表单标签中的按钮指向与表单不同的动作/动作类,但它不起作用.我在另一个线程中读到这是由于 Struts 2 中的一个错误,应该设置 struts.mapper.action.prefix.enabled"="true",所以我做到了,但它仍然是一样的.

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.

这行得通,

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

(动作)

public class PrintItem extends BaseAction {

    @Override
    public String perform() throws Exception {

        doPrintLogic();
        return SUCCESS;

    }

    public String cancel(){

        return "CANCEL";

    }
}

但如果我将 struts.xml 中的取消"动作映射更改为

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>

没有

这正常吗?是否可以从已经映射到一个表单的表单中映射到不同的操作类?

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

推荐答案

这是正常的,因为您只能将表单映射到由表单标签的 action 属性指定的一个动作(在 HTML 中是历史性的).提交按钮不会更改该映射,而是会调整动作映射器以使用不同的动作,因为启用了前缀.因此,如果您需要更改此行为,您可以动态更改表单映射或更改默认操作映射器等.但是,如果操作实例已经创建,则会出现问题.因此,您应该使用默认操作映射器.但是您的帖子 struts.mapper.action.prefix.enabled"="true" 中有一个错字.启用提交按钮操作的常量是

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

或者如果你使用 struts.properties

struts.mapper.action.prefix.enabled=true

相关文章