java Struts2 以单一形式处理多个动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14466733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:23:00  来源:igfitidea点击:

Struts2 handle multiple actions in single form

javajspstruts2ognldmi

提问by gmeka

I have a form contains three buttons print/export/save.

我有一个表单包含三个按钮print/ export/ save

<s:form action="/userAction">
    <s:submit type="image" value="%{'print'}" src="/print.png" />
    <s:submit type="image" value="%{'export'}" src="/export.png" />
    <s:submit type="image" value="%{'save'}" src="/save.png" />
</s:form>

How can I map this in struts.xml?

我怎样才能把它映射到struts.xml

采纳答案by Roman C

In the struts.xmlthe action is mapped via the <actiontag

struts.xml动作是通过<action标签映射的

<action name="userAction" class="...

the submit buttons should include methodattribute to call corresponding methods of the action

提交按钮应包含method属性以调用相应的操作方法

<s:submit type="image" value="%{'print'}" src="/print.png" method="print" />
<s:submit type="image" value="%{'export'}" src="/export.png" method="export" />
<s:submit type="image" value="%{'save'}" src="/save.png" method="save" />

回答by Aleksandr M

In order to use methodattribute of the <s:submit>tag DynamicMethodInvocationmust be enabled. Another solution is to use actionattribute.

为了使用标签的method属性,必须启用。另一种解决方案是使用属性。<s:submit>DynamicMethodInvocationaction

In JSP:

在 JSP 中:

<s:form action="save">
    <s:submit type="image" value="%{'print'}" src="/print.png" action="print" />
    <s:submit type="image" value="%{'export'}" src="/export.png" action="export" />
    <s:submit type="image" value="%{'save'}" src="/save.png" />
</s:form>

In struts.xml:

在 struts.xml 中:

<action name="print" class="...">
  <result>...</result>
</action>
<action name="export" class="...">
  <result>...</result>
</action>
<action name="save" class="...">
  <result>...</result>
</action>