Struts : DispatchAction 的使用
来源:互联网 发布:java中compare2 编辑:程序博客网 时间:2024/04/28 14:24
今天在看一个 Struts代码时,发现 Action类继承的父类为 DispatchAction,于是找了找资料研究了下 DispatchAction类,现总结如下:
DispatchAction 是 Struts1.1 中的一个类,它的父类是 Action ,它的作用就在于将多个功能相似的业务逻辑放在同一个 Action 中实现,各个业务逻辑通过传入不同的参数来决定执行哪个操作方法
通常在 Action中我们都是通过 execute方法来处理业务逻辑及页面转向,一个 Action 只能完成一种业务逻辑处理 , 当然我们也可以在页面插入一个隐藏的变量,然后在 Action 的 execute方法中通过判断这个隐藏变量的值来决定调用哪个方法,也可以达到同一个 Action 来处理多种业务逻辑,可是这样的话想一想肯定会造成页面代码的增加及影响页面代码的可读性.
看一看 DispatchAction是如何实现的
比如对一个用户对象来说,存在增加,删除,修改的操作,首先创建一个继承 DispatchAction 的 UserAction 类,
然后将 addUser,delUser,updateUser这些方法写在这个类里面,代码如下:
packagecom.why.struts.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.actions.DispatchAction;
importcom.why.Constant;
importcom.why.struts.form.AddUserForm;
importcom.why.struts.form.LoginForm;
publicclass UserActionextends DispatchAction
{
publicActionForward addUser (ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
//增加用户业务的逻辑
returnmapping.findForward(Constant.FORWARD_ADD );
}
publicActionForward delUser(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
//删除用户业务的逻辑
returnmapping.findForward(Constant.FORWARD_SUCCESS );
}
publicActionForward updateUser(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
//更新用户业务的逻辑
returnmapping.findForward(Constant.FORWARD_SUCCESS );
}
}
如何实现这些不同方法的调用呢 ? 那就是要在 struts-config.xml文件中更改 action-mapping的配置,如下:
< action-mappings >
<action
attribute= "addUserForm"
input= "/addUser.jsp"
name= "addUserForm"
parameter="method"
path= "/addUser"
scope= "request"
type="com.why.struts.action.UserAction" >
</action >
<action
attribute= "delUserForm"
input= "/delUser.jsp"
name= "delUserForm"
parameter="method"
path= "/delUser"
scope= "request"
type="com.why.struts.action.UserAction" />
< action
attribute= "updateUserForm"
input= "/updateUser.jsp"
name= "updateUserForm"
parameter="method"
path= "/updateUser"
scope= "request"
type="com.why.struts.action.UserAction" />
</action-mappings >
可以看到每个 <action /> 中都增加了 parameter=" "项,这个值可以随便命名,如上面命名为 metho d ,用来接收页面传来的参数
如下为页面的提交, 注意:对应<action /> 中的parameter 值 , 对应UserAction 类中的方法名
AddUser.jsp
<html:link href="addUser.do?method=addUser">Add User</html:link>
DelUser.jsp
<html:link href="delUser.do?method=delUser">Add User</html:link>
UpdateUser.jsp
<html:link href="updateUser.do?method=updateUser">Add User</html:link>
- Struts : DispatchAction 的使用
- Struts中DispatchAction的使用.
- struts中DispatchAction的使用
- Struts : 使用 DispatchAction
- Struts心得--DispatchAction使用日记
- Struts心得--DispatchAction使用日记
- struts中使用DispatchAction可能会遇到的一个问题
- struts的DispatchAction的应用
- Struts中DispatchAction的用法
- Struts的DispatchAction简单应用
- Struts中DispatchAction的应用
- Struts常用的Action--DispatchAction
- Struts -- DispatchAction
- struts DispatchAction
- struts DispatchAction
- Struts-DispatchAction
- DispatchAction的使用
- DispatchAction 的使用
- JAVA获取随机数
- 防范JAVA内存泄漏解决方案
- 窗体间数据传递的问题
- JAVA 程序中如何拷贝一个目录下的文件及子目录到另一个目录,如何获取系统环境变量等...
- ORACLE SEQUENCE的简单介绍
- Struts : DispatchAction 的使用
- 关于document.cookie的使用
- 使用SOAP頭條目實現用戶名和密碼驗證
- 傻女孩离幸福最近
- 开户公告
- 从网上看到的另一个C#串口通讯类(JustinIO)
- Spring Bean包装类案例之类型转换
- SuSE 新手指南(安装各类软件/驱动/启动xgl......)
- j2sdk 的配置