论坛首页 Java企业应用论坛

Struts1.x重要类之DispatchAction

浏览 6072 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-16   最后修改:2009-04-16
 在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是什么了,呵呵

   发表时间:2009-04-17  
DistpatchAction 个人觉得也不是很好用,在配合验证框架的时候有点麻烦。。当然这也是在某种场合下。。有时候确实还是不错的。
0 请登录后投票
   发表时间:2009-04-17  
并不适合说有情况,但有时候还是需要用的,毕竟可以减少Action的数量,并且在一定程度上可以使代码结构更清晰
0 请登录后投票
   发表时间:2009-04-17  
1.2.9版本以后还有个EventDispatcherAction更好用
0 请登录后投票
   发表时间:2010-03-05  
DispatchAction在我的应用中是被大量使用,还不错。配合Spring的struts.DelegatingActionProxy类管理action,清晰了很多
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics