快乐编程-知识分享-Struts1.x学习-常用Action-004

目标:学会使用struts1.x的四种常用的Action,派发技术的使用

推荐:推荐使用DispatchAction类

对象:适合自学者、初学者、兴趣爱好者。

理念: 先行动(Coding),后理解(Thinking) ; 在最短的时间内采取最大量的Coding 。 分享越多,收获越大

--------------------------------------------------------------------------------------------------------------------------------

一、继承Action类,java代码如下:

package com.raky.train.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
/**
 * struts1.x UserAction
 * 
 * @author raky
 * @version v1.0
 * 
 */
public class UserAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String method = request.getParameter("method");
		if(null != method && method.length() > 0){
			if(method.equals("add")){
				return this.add(mapping, form, request, response);
			}
			if(method.equals("update")){
				return this.update(mapping, form, request, response);
			}
			if(method.equals("delete")){
				return this.delete(mapping, form, request, response);
			}
		}
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action1添加方法");
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action1更新方法");
		return mapping.findForward("success");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action1删除方法");
		return mapping.findForward("success");
	}
}

 页面代码,如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<c:set var="ctx" value="${pageContext.request['contextPath']}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - action1 - Test1 page</title>
<script type="text/javascript">
    function submitForm(methodValue) {
        document.forms[0].action = "${ctx}/userAction.do?method=" + methodValue;
        document.forms[0].submit();
    }
</script>
</head>
<body>
<h2>raky - struts1.x - action1</h2>
<html-el:form action="/userAction" method="post">
<table>
	<tr>
		<td align="center" colspan="2">
			<html-el:submit property="submitAdd" onclick="submitForm('add')" value="添加" />
			<html-el:submit property="submitUpdate" onclick="submitForm('update')" value="修改" />
			<html-el:submit property="submitDelete" onclick="submitForm('delete')" value="删除" />
			<html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" />
		</td>	
	</tr>
</table>
</html-el:form>
</body>
</html>

 二、继承DispatchAction,java代码如下:

package com.raky.train.action;

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.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
/**
 * struts1.x UserDispatchAction
 * 
 * @author raky
 * @version v1.0
 * 
 */
public class UserDispatchAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action2添加方法");
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action2更新方法");
		return mapping.findForward("success");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action2删除方法");
		return mapping.findForward("success");
	}
}

 页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<c:set var="ctx" value="${pageContext.request['contextPath']}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - action2 - Test2 page</title>
<script type="text/javascript">
    function submitForm(methodValue) {
        document.forms[0].action = "${ctx}/userDispatchAction.do?method=" + methodValue;
        document.forms[0].submit();
    }
</script>
</head>
<body>
<h2>raky - struts1.x - action2</h2>
<html-el:form action="/userDispatchAction" method="post">
<table>
	<tr>
		<td align="center" colspan="2">
			<html-el:link href="${ctx}/userDispatchAction.do?method=add">添加</html-el:link>
			<html-el:link href="${ctx}/userDispatchAction.do?method=update">修改</html-el:link>
			<html-el:link href="${ctx}/userDispatchAction.do?method=delete">删除</html-el:link>
			<html-el:link href="${ctx}/">返回</html-el:link>
		</td>	
	</tr>
</table>
</html-el:form>
</body>
</html>

 三、继承EventDispatchAction,java代码如下:

package com.raky.train.action;

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.action.DynaActionForm;
import org.apache.struts.actions.EventDispatchAction;
/**
 * struts1.x UserLookupDispatchAction
 * 
 * @author raky
 * @version v1.0
 * 
 */
public class UserEventDispatchAction extends EventDispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action3添加方法");
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action3更新方法");
		return mapping.findForward("success");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action3删除方法");
		return mapping.findForward("success");
	}
}

 页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<c:set var="ctx" value="${pageContext.request['contextPath']}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - action3 - Test3 page</title>
</head>
<body>
<h2>raky - struts1.x - action3</h2>
<form action="${ctx}/userEventDispatchAction.do" method="post">
<table>
	<tr>
		<td align="center" colspan="2">
			<html-el:submit property="add" value="添加" />
			<html-el:submit property="update" value="修改" />
			<html-el:submit property="delete" value="删除" />
			<html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" />
		</td>	
	</tr>
</table>
</form>
</body>
</html>

 四、继承LookupDispatchAction,java代码如下:

package com.raky.train.action;

import java.util.HashMap;
import java.util.Map;

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.action.DynaActionForm;
import org.apache.struts.actions.LookupDispatchAction;
/**
 * struts1.x UserLookupDispatchAction
 * 
 * @author raky
 * @version v1.0
 * 
 */
public class UserLookupDispatchAction extends LookupDispatchAction {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("dynaForm.add", "add");
		map.put("dynaForm.update", "update");
		map.put("dynaForm.delete", "delete");
		return map;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action4添加方法");
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action4更新方法");
		return mapping.findForward("success");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaActionForm dynaForm = (DynaActionForm) form;
		dynaForm.set("message", "执行action4删除方法");
		return mapping.findForward("success");
	}
}

 资源文件代码如下:

dynaForm.add = add
dynaForm.update = update
dynaForm.delete = delete

页面代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<c:set var="ctx" value="${pageContext.request['contextPath']}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raky train struts1.x - action4 - Test4 page</title>
<script type="text/javascript">
    function submitForm(methodValue) {
        document.forms[0].action = "${ctx}/userLookupAction.do?method=" + methodValue;
        document.forms[0].submit();
    }
</script>
</head>
<body>
<h2>raky - struts1.x - action4</h2>
<html-el:form action="/userLookupAction" method="post">
<table>
	<tr>
		<td align="center" colspan="2">
			<input type="submit" name="submitAdd" onclick="submitForm('add')" value="添加" />
			<input type="button" name="btnUpdate" onclick="submitForm('update')" value="修改" />
			<input type="submit" name="submitDelete" onclick="submitForm('delete')" value="删除" />
			<html-el:button property="btnValue" value="返回" onclick="location.href=${ctx}/" />
		</td>	
	</tr>
</table>
</html-el:form>
</body>
</html>

总体strutsConfig.xml配置文件代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
	<!-- =================== Form Bean Definitions ==================== -->
	<form-beans>
		<form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm">
			<form-property name="message" type="java.lang.String" />
		</form-bean>
	</form-beans>
	<!-- ================= Global Forward Definitions ================= -->
	<global-forwards>
		<forward name="success" path="/success.jsp" />
	</global-forwards>
	<!-- =================== Action Definitions ==================== -->
	<action-mappings>
		<action input="/action/test1.jsp" name="dynaForm"
			path="/userAction" scope="request"
			type="com.raky.train.action.UserAction" validate="false" />
  	
		<action input="/action/test2.jsp" parameter="method" name="dynaForm"
			path="/userDispatchAction" scope="request"
			type="com.raky.train.action.UserDispatchAction" validate="false" />
		
		<action input="/action/test3.jsp" parameter="add,update,delete" name="dynaForm"
			path="/userEventDispatchAction" scope="request"
			type="com.raky.train.action.UserEventDispatchAction" validate="false" />
		
		<action input="/action/test4.jsp" parameter="method" name="dynaForm"
			path="/userLookupAction" scope="request"
			type="com.raky.train.action.UserLookupDispatchAction" validate="false" />

	</action-mappings>

	<!-- ================ Message Resources Definitions ================ -->
	<message-resources parameter="MessageResources" />
	
</struts-config>

总结:本文基本上总结了struts1.x常用的Action使用技术和技巧。

相关推荐