【问题标题】:Is there a different way to configuring actions in Struts 2?在 Struts 2 中配置动作有不同的方法吗?
【发布时间】:2018-01-24 12:59:37
【问题描述】:

我需要将遗留(Struts 1)代码迁移到 Struts2。

如果同一个动作类中有多个方法,我们可以在单个<action>标签中配置它们吗?

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">

<action name="product" 
        class="com.ProductAction"
        method="show">
<result name="success">welcome.jsp</result>
</action>

<action name="product" 
        class="com.ProductAction"
        method="showErr">
<result name="error">error.jsp</result>
</action>

</package>
</struts>    

在这里,我有单个动作,即product 和单个动作类,即ProductAction。那么,我可以在单个&lt;action&gt; 标签中配置这两种方法(showshowErr)吗?

【问题讨论】:

    标签: struts2 struts-config


    【解决方案1】:

    如果在同一个包中使用,动作名称将被覆盖。动作名称映射到特定方法或execute

    您可以使用通配符映射将方法名称放入操作中。

    <action name="*product" class="com.ProductAction" method="{1}">
    

    【讨论】:

    • 在这种情况下,各个结果如何绑定到调用的方法。例如,如果我的 Action 类中有 5 个方法,我将不得不绑定 5 个结果。
    • 您可以在结果中使用通配符。见this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2014-09-18
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多