引言
小編覺得代碼就是要不斷的簡潔,不斷的加強,便於維護,這就是我們的目標!!
DispatcherAction
當我們有多個功能相似的業務,使用Action的時候,就需要建立很多個Action類,而且需要在struts-config.xml文件中配置多個<action>標籤。其中好的的重複工作不說,看着就讓人覺得不舒服……
所以,我們可以使用DispatcherAction幫助我們這些相似的功能方法一個Action中,各個業務邏輯通過傳入不同的參數來決定執行哪個方法。
DispatcherAction的使用
1、DispatcherAction中的execute方法,如果覆蓋必須顯示調用super.execute()
2、Parameter參數值不能是execute和perform
3、<action>標籤中的parameter屬性
Demo
1、建立一個UserAction 類,繼承DispatcherAction類
package com.bjpowernode.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 UserAction extends DispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// return mapping.findForward("add_success");
return mapping.findForward("success");
}
public ActionForward del(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// return mapping.findForward("del_success");
return mapping.findForward("success");
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// return mapping.findForward("update_success");
return mapping.findForward("success");
}
}
2、配置struts-config.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<action path="/user/*"
type="com.bjpowernode.struts.UserAction"
parameter="command"
>
<forward name="success" path="/user/{1}_success.jsp"/>
</action>
</action-mappings>
</struts-config>
3、建立jsp頁面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="user/add.do?command=add">添加</a>
<a href="user/del.do?command=del">刪除</a>
<a href="user/update.do?command=update">修改</a>
</body>
</html>
注意:頁面上的url傳的參數command與配置文件中<action>標籤中的parameter屬性要一致!!
<%@ 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>
add_success.jsp
</body>
</html>
del_success.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>
del_success.jsp
</body>
</html>
update_success.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>
update_success.jsp
</body>
</html>
4、效果
總結
向着我們的目標前進!!