`
ideaforj
  • 浏览: 73848 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struts1.x重要类之DispatchAction

阅读更多
 在Struts的开发之中DispatchAction有着广泛的应用,应次了解DispatchAction也是很重要的。 

DispatchAction是一个抽象的Action,它根据request 中的parameter参数来执行相应的方法。通个这个Action类可以将不同的Action集中到一个Action文件中来。 
下面通过实际的代码来了解DispatchAction 
1.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
 <h1>Struts DispatchAction测试</h1>
	<a href='<html:rewrite action="/which_pet_dispatchAction"/>?mymethod=dog'>Dog</a>
 	<a href='<html:rewrite action="/which_pet_dispatchAction"/>?mymethod=bird'>Bird</a>
 	<a href='<html:rewrite action="/which_pet_dispatchAction"/>?mymethod=fish'>fish</a>
 	<a href="javascript:window.location.href='<html:rewrite action="/which_pet_dispatchAction"/>?mymethod=cat'">cat</a>
  </body>
</html>

DispatchAction的代码

package com.struts;

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 TestDispatchAction extends DispatchAction {

	public ActionForward dog(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("pet", "dog");
		return mapping.findForward("result");
	}
	
	public ActionForward bird(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("pet", "bird");
		return mapping.findForward("result");
	}
	
	public ActionForward cat(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("pet", "cat");
		return mapping.findForward("result");
	}
	
	public ActionForward fish(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("pet", "fish");
		return mapping.findForward("result");
	}

}

 3.struts的配置文件

  <action-mappings>
	<action path="/which_pet_dispatchAction"
  			type="com.struts.TestDispatchAction"
  			scope="request"
  			parameter="mymethod">
  			<forward name="result" path="/WEB-INF/page/showpet.jsp"/>
  	</action>
  </action-mappings>

4.显示结果的页面showpet.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<h1>
		你养的宠物是:${pet }
	</h1>
</body>
</html>

 在运行上面的代码的时候,点击不同的动物,因为URL的参数(mymethod)是各不相同的,struts就根据传过来的参数判断执行不同的方法...

 这下struts就知道你养的pet是什么了,呵呵

分享到:
评论
4 楼 peng3409 2010-03-05  
DispatchAction在我的应用中是被大量使用,还不错。配合Spring的struts.DelegatingActionProxy类管理action,清晰了很多
3 楼 tangbo530 2009-04-17  
1.2.9版本以后还有个EventDispatcherAction更好用
2 楼 http_url 2009-04-17  
并不适合说有情况,但有时候还是需要用的,毕竟可以减少Action的数量,并且在一定程度上可以使代码结构更清晰
1 楼 zhxing 2009-04-17  
DistpatchAction 个人觉得也不是很好用,在配合验证框架的时候有点麻烦。。当然这也是在某种场合下。。有时候确实还是不错的。

相关推荐

    Struts+1.x文件的上传和下载.txt

    在Struts 1.x版本中,文件的上传与下载是一项常见且重要的功能。 ### 文件上传 文件上传主要通过`FileAction`类来实现。在Struts框架中,`FileAction`继承自`DispatchAction`,并实现了文件上传的相关逻辑。首先,...

    struts2教程

    Struts2在Struts1.x的基础上进行了改进,采用了WebWork框架的一些设计理念。它的MVC架构更加灵活,增强了类型安全性和异常处理能力。 五、Struts2的主要组件及作用 1. FilterDispatcher:作为新的中央控制器,是一...

    详细的Struts2的实例教程

    Struts2框架是对Struts1.x的重要升级,它引入了拦截器模型和依赖注入等概念,使其更加灵活和强大。 - **拦截器模型**:Struts2采用拦截器栈的方式处理请求,每个拦截器可以对请求进行预处理和后处理。 - **依赖注入...

    struts 1的标签的用法详细

    ### 一、STRUTS1.X 工作原理 Struts 1 的工作流程主要包括以下几个步骤: 1. 用户发起HTTP请求。 2. 请求被Struts的前端控制器`ActionServlet`截获。 3. `ActionServlet`根据`struts-config.xml`配置文件解析请求,...

    很好的Struts2教程

    3. **struts.xml** - 核心配置文件,类似于Struts1.x的Struts-config.xml,但更加灵活,支持更多的配置选项,如Action、Result等的定义。 4. **Result** - 类似于Struts1.x的Forward,但提供了更丰富的视图技术和...

    速动画教程系列第13集

    DispatchAction是Struts 1.x版本中的一个关键类,它的主要职责是将HTTP请求映射到特定的方法来执行。在传统的Struts中,当用户发起一个请求时,请求会被Struts的控制器Servlet(ActionServlet)接收,然后根据配置的...

Global site tag (gtag.js) - Google Analytics