struts2配置文件中的method={1}详解

struts.xml中的配置:
<!-- 配置用户模块的action -->
    <action name="user_*" class="userAction" method="{1}">
        <result name="registPage">/WEB-INF/jsp/regist.jsp</result>
    </action>

/*
此时method={1}中的{1}代表user_*中的*,即加入你访问路径是/user_registPage.action,则此刻访问的是该Action中的registPage方法。同理,如果通配符* == delete,则就访问的是delete方法。
当name中含有多个通配符的时候,method={2} ,就代表第二个通配符,同理以此类推。
这种方式更灵活的简化了struts.xml的配置文件。
*/


regist.jsp页面中的代码段:
<li id="headerRegister" class="headerRegister" style="display: list-item;">
                    <a href="${ pageContext.request.contextPath }/user_registPage.action">注册</a>|
                </li>

/*
user_registPage.action对应 name="user_*",通配符*就是registPage,
method="{1}"里面的{1}就是代表registPage()方法,
这个registPage()方法存在于 UserAction中

*/

Action类:

public class UserAction extends ActionSupport{
    /**
     * 跳转到注册页面的执行方法
     */
public String registPage(){            
        return "registPage";
    }
}

<action name="*_*" method="{2}" class="action.{1}Action">

上面代码定义了一个模式为*_*的action,只要匹配该模式的请求,都可以被该Action处理。

如果有URL为BOOK_save.action请求,因为匹配了*_*模式,第一个*为BOOK,第二个8为save,

则调用action.BOOKAction处理类中的save()方法来处理用户请求。

原文地址:https://www.cnblogs.com/fifiyong/p/6027565.html