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是什么了,呵呵

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值