SSH之action中的多个方法的调用方法
public String logon(){ return "success"; } public String register(){ return"success"; }
有三种方式可以将页面提交和action的方法对应。
(一)动态方法调用,配置文件不变,一个action类对应配置文件中一个action标签,表单提交的action不直接等于某个action的name,而是以action的name!action的方法名来提交。
1.配置文件
<action name="user" class="com.tgb.struts2.action.LogonAction" > <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action>
2. 表单提交
登录:<form id="form" action="user!logon" method="post"> 注册:<form id="form" action="user!register" method="post">
(二) 修改配置文件,将同一个action中的每个方法都用一个action标签映射
1.配置文件
<action name="logon" class="com.tgb.struts2.action.LogonAction" > <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> <action name="register" class="com.tgb.struts2.action.LogonAction" > <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action>
2. 表单提交
登录:<form id="form" action="logon" method="post"> 注册:<form id="form" action="register" method="post">
(三) 使用通配符映射方式
1. 配置文件
<action name="*User_*" class="com.tgb.struts2.action.LogonAction" method="{1}" > <result name="success">/{2}.jsp</result> <result name="error">/error.jsp</result> </action>
method={1}表示对应使用的方法为第一个*的名字,{2}.jsp表示跳转到名字为第二个*的名字的地址
2. 表单提交
<form id="form" action="logonUser_index" method="post"> <!-- 执行action中的logon方法,执行成功后返回index首页面 --> <form id="form" action="registerUser_logon" method="post"> <!--执行action中的register方法,执行成功后返回logon登录页面 -->
总结: 这三种方式中,使用!会暴露所使用的框架,第二种方式会使配置文件变得复杂冗余,第三种方式中和了前两种方式的缺点,建议使用通配符映射方式。