struts一个action处理多个方法

在实际开发中,让一个业务逻辑对应一个Acton类是不现实的,通常我们都是在一个action中定义多个方法的。

下面了解一下strtus1和struts2在一个action中处理多个方法的实现。

struts1:

struts1中一个action处理多个方法,通过让action继承DispachAction来实现

1. action继承DispachAction,并实现多个方法的业务逻辑

	public class LogonAction extends DispatchAction {

public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//业务逻辑
return mapping.findForward("success");
}
public ActionForward logon(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
return mapping.findForward("success");
}

public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
return mapping.findForward("success");
}



2. strtus-config.xml配置文件中,每个action标签后面,加上parameter=""这个属性

		<action path="/user" type="com.tgb.struts1.LogonAction" name="logonForm"  parameter="method">
<forward name="success" path="/success.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>


3. 页面提交时确定执行action中的哪个方法,如果不指定,则执行action中的unspecified()方法.

		<form action="user" mehtod="post">
<input type="hidden" name="method" value="logon">



struts2:

action中定义多个方法:

    public String logon(){
return"success";
}

publicString 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>


2. 表单提交

	<form id="form" action="logonUser_index" method="post"><!-- 执行action中的logon方法,执行成功后返回index首页面 -->
    <form id="form" action="registerUser_logon" method="post"><!--执行action中的register方法,执行成功后返回logon登录页面  -->


这三种方式中,使用!会暴露所使用的框架,第二种方式会使配置文件变得复杂冗余,第三种方式中和了前两种方式的缺点,建议使用通配符映射方式。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值