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是什么了,呵呵
相关推荐
lupeng 2020-11-14
sjcheck 2020-11-10
meylovezn 2020-08-28
owhile 2020-08-18
Francismingren 2020-08-17
pythonclass 2020-07-29
sunzhihaofuture 2020-07-19
爱读书的旅行者 2020-07-07
行吟阁 2020-07-05
tianqi 2020-07-05
行吟阁 2020-07-04
冰蝶 2020-07-04
lyg0 2020-07-04
owhile 2020-07-04
opspider 2020-06-28
lengyu0 2020-06-28
tianqi 2020-06-21
dadaooxx 2020-06-16