Struts 2表单标签中的多个提交按钮 [英] Multiple submit buttons in Struts 2 form tag

查看:244
本文介绍了Struts 2表单标签中的多个提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将表单标签中的按钮指向与表单不同的action/action类,但是它不起作用.我在另一个线程中读到,这是由于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?

推荐答案

这是正常的,因为您可以将表单映射到仅由form标记的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

这篇关于Struts 2表单标签中的多个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆