我是靠谱客的博主 烂漫银耳汤,这篇文章主要介绍Struts1的DispatchAction()方法,现在分享给大家,希望可以做个参考。

在早期的Struts 1.x中为了在大的工程下方便管理而添加了DispatchAction()方法

DispatchAction继承与Action类,是一个抽象类,封装了一些基础方法,来解决使用一个Action处理多个操作的能力,这就是DispatchAction最大的用途,它可以帮助我们用一个Action类,封装一套类似的操作方法,节省了类的数目,同时也减轻了后期维护的困难。

DispatchAction中主要包括以下几个方法:

        protected ActionForward dispatchMethod

  protected java.lang.reflect.Method getMethod

 

DispatchAction的使用有三步骤:

1.写一个类继承(extends)DispatchAction

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class UserLogin extends DispatchAction{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("主要的方法"); return super.execute(mapping, form, request, response); } public ActionForward Login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("这个是Login"); return mapping.findForward("welcome"); } }

2.配置struts-comfig.xml文件

复制代码
1
2
3
4
5
6
7
8
9
10
<action-mappings > <action path="/UserLogin" name="userForm" type="com.action.UserLogin" parameter="action"> <forward name="welcome" path="/welcome.jsp"></forward> <forward name="error" path="/index.jsp"></forward> </action> </action-mappings>

parameter的属性值是可以任意起的,只要你记得在传参数的时候统一就可以了。

3.调用自定义方法

可以有两种方式来调用:

第一种是以表单的方式

复制代码
1
<form action="UserLogin.do?action=Login" method="post"></form>

注意上面parameter的值和这里所使用的要一样,也就是上面所说的统一

第二种是隐藏域的形式

     <input type="hidden" name="oparator" value="Login" />

  然后用表单提交 那么 Struts xml配置文件中就改成

  <action path="/UserLogin" name="userForm" type="com.action.UserLogin" parameter="oparator">

  配置文件中的parameter 值就是jsp页面隐藏域的值

  其他的和Action的配置没有太大的区别 这里的名字是随便取的

 

 

 

最后

以上就是烂漫银耳汤最近收集整理的关于Struts1的DispatchAction()方法的全部内容,更多相关Struts1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部