注意:以下两个例子在调试的过程中用的是同一个form和struts-config.xml,在调试的过程中DispatchAction是不用资源文件ApplicationResources.properties的,而LookupDispatchAction是要用到ApplicationResources.properties
先来说说DispatchAction,网上的许多例子都是和超链接来做的,其实用DispatchAction用按钮也是可以实现的,来看例子吧!
UserManagement.jsp
< %@ taglib uri ="http://struts.apache.org/tags-logic" prefix ="logic"% >
< %@taglib prefix ="c" uri ="http://java.sun.com/jsp/jstl/core"% >
< html >
< head >
< title >DispatchAction Example - viralpatel.net </title>
</head>
< body >
< h2 >
User Management (DispatchAction Example)
</h2>
< font color ="black" > < h3 >
用struts标签的方法
</h3> </font>
< html:form action ="/user" method ="post" >
< html:text property ="userName" />
< html:submit property ="method" value ="create" />
< html:submit property ="method" value ="delete" />
</html:form>
< font color ="black" > < h3 >
不用struts标签的方法
</h3> </font>
< form action ="/struts_action_test/user.do" method ="post" >
< input type ="text" name ="userName" />
< input type ="submit" name ="method" value ="create" />
< input type ="submit" name ="method" value ="delete" />
</form>
< font color ="blue" > < h3 >
${message }
</h3> </font> 现有以下用户:
< c:forEach var ="numArray" items ="${num}" >
< table >
< tr >
< td >
< c:out value ="${numArray}" />
</td>
</tr>
</table>
</c:forEach>
</body>
</html>
< %@taglib prefix ="c" uri ="http://java.sun.com/jsp/jstl/core"% >
< html >
< head >
< title >DispatchAction Example - viralpatel.net </title>
</head>
< body >
< h2 >
User Management (DispatchAction Example)
</h2>
< font color ="black" > < h3 >
用struts标签的方法
</h3> </font>
< html:form action ="/user" method ="post" >
< html:text property ="userName" />
< html:submit property ="method" value ="create" />
< html:submit property ="method" value ="delete" />
</html:form>
< font color ="black" > < h3 >
不用struts标签的方法
</h3> </font>
< form action ="/struts_action_test/user.do" method ="post" >
< input type ="text" name ="userName" />
< input type ="submit" name ="method" value ="create" />
< input type ="submit" name ="method" value ="delete" />
</form>
< font color ="blue" > < h3 >
${message }
</h3> </font> 现有以下用户:
< c:forEach var ="numArray" items ="${num}" >
< table >
< tr >
< td >
< c:out value ="${numArray}" />
</td>
</tr>
</table>
</c:forEach>
</body>
</html>
UserManagementAction
package com.zgxr.struts;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
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 UserManagementAction extends DispatchAction {
// 用于存放添加的用户
List list = new ArrayList();
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm loginActionForm = (LoginActionForm) form;
// 将新添加的用户放入list
list.add(loginActionForm.getUserName());
request.setAttribute( "message", "User created successfully" + ":"
+ "当前用户数量为:" + list.size());
request.setAttribute( "num", list);
return mapping.findForward( "success");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm loginActionForm = (LoginActionForm) form;
//将提交上来的用户从List中删除
list.remove(loginActionForm.getUserName());
request.setAttribute( "message", "User deleted successfully" + ":"
+ "当前用户数量为:" + list.size());
request.setAttribute( "num", list);
return mapping.findForward( "success");
}
}
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
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 UserManagementAction extends DispatchAction {
// 用于存放添加的用户
List list = new ArrayList();
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm loginActionForm = (LoginActionForm) form;
// 将新添加的用户放入list
list.add(loginActionForm.getUserName());
request.setAttribute( "message", "User created successfully" + ":"
+ "当前用户数量为:" + list.size());
request.setAttribute( "num", list);
return mapping.findForward( "success");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm loginActionForm = (LoginActionForm) form;
//将提交上来的用户从List中删除
list.remove(loginActionForm.getUserName());
request.setAttribute( "message", "User deleted successfully" + ":"
+ "当前用户数量为:" + list.size());
request.setAttribute( "num", list);
return mapping.findForward( "success");
}
}
LoginActionForm:
package com.zgxr.struts;
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm {
// 定义两个变量,这两个值与页面上的html:text的property的值相对应
private int numOne;
private int numTwo;
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getNumOne() {
return numOne;
}
public void setNumOne( int numOne) {
this.numOne = numOne;
}
public int getNumTwo() {
return numTwo;
}
public void setNumTwo( int numTwo) {
this.numTwo = numTwo;
}
}
import org.apache.struts.action.ActionForm;
public class LoginActionForm extends ActionForm {
// 定义两个变量,这两个值与页面上的html:text的property的值相对应
private int numOne;
private int numTwo;
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getNumOne() {
return numOne;
}
public void setNumOne( int numOne) {
this.numOne = numOne;
}
public int getNumTwo() {
return numTwo;
}
public void setNumTwo( int numTwo) {
this.numTwo = numTwo;
}
}
struts-config.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-beans >
< form-bean name ="loginActionForm"
type ="com.zgxr.struts.LoginActionForm" >
</ form-bean >
</ form-beans >
< action-mappings >
< action path ="/user" parameter ="method" name ="loginActionForm"
type ="com.zgxr.struts.UserManagementAction" >
< forward name ="success" path ="/UserManagement.jsp" />
< forward name ="failure" path ="/UserManagement.jsp" />
</ action >
< action path ="/test" name ="loginActionForm" scope ="request"
parameter ="action" type ="com.zgxr.struts.TestAction"
input ="/operation.jsp" >
< forward name ="success" path ="/operation.jsp" />
< forward name ="failure" path ="/operation.jsp" />
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
<!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-beans >
< form-bean name ="loginActionForm"
type ="com.zgxr.struts.LoginActionForm" >
</ form-bean >
</ form-beans >
< action-mappings >
< action path ="/user" parameter ="method" name ="loginActionForm"
type ="com.zgxr.struts.UserManagementAction" >
< forward name ="success" path ="/UserManagement.jsp" />
< forward name ="failure" path ="/UserManagement.jsp" />
</ action >
< action path ="/test" name ="loginActionForm" scope ="request"
parameter ="action" type ="com.zgxr.struts.TestAction"
input ="/operation.jsp" >
< forward name ="success" path ="/operation.jsp" />
< forward name ="failure" path ="/operation.jsp" />
</ action >
</ action-mappings >
< message-resources parameter ="ApplicationResources" />
</ struts-config >
好了把以上这些拷下来试试吧,下面我们来看看LookupDispatchAction
operation.jsp
%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
< %@ taglib uri ="http://struts.apache.org/tags-html" prefix ="html"% >
< %@ taglib prefix ="bean" uri ="http://struts.apache.org/tags-bean"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title >LookupDispatchAction Example - viralpatel.net </title>
</head>
< body >
< h2 >
User Management (LookupDispatch Action Example)
</h2>
用struts标签的方法
< html:form action ="/test" method ="post" >
< html:text property ="numOne" />
< html:text property ="numTwo" />
< html:submit property ="action" >
< bean:message key ="button.sum" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.minus" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.multiply" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.divide" />
</html:submit>
</html:form>
不用struts标签的方法
< form name ="loginActionForm" method ="post" action="/struts_action_test/test.do >
< input type ="text" name ="numOne" />
< input type ="text" name ="numTwo" />
< input type ="submit" name ="action" value ="sum" />
< input type ="submit" name ="action" value ="minus" >
< input type ="submit" name ="action" value ="multiply" />
< input type ="submit" name ="action" value ="divide" />
</form>
</body>
</html>
< %@ taglib uri ="http://struts.apache.org/tags-html" prefix ="html"% >
< %@ taglib prefix ="bean" uri ="http://struts.apache.org/tags-bean"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
< title >LookupDispatchAction Example - viralpatel.net </title>
</head>
< body >
< h2 >
User Management (LookupDispatch Action Example)
</h2>
用struts标签的方法
< html:form action ="/test" method ="post" >
< html:text property ="numOne" />
< html:text property ="numTwo" />
< html:submit property ="action" >
< bean:message key ="button.sum" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.minus" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.multiply" />
</html:submit>
< html:submit property ="action" >
< bean:message key ="button.divide" />
</html:submit>
</html:form>
不用struts标签的方法
< form name ="loginActionForm" method ="post" action="/struts_action_test/test.do >
< input type ="text" name ="numOne" />
< input type ="text" name ="numTwo" />
< input type ="submit" name ="action" value ="sum" />
< input type ="submit" name ="action" value ="minus" >
< input type ="submit" name ="action" value ="multiply" />
< input type ="submit" name ="action" value ="divide" />
</form>
</body>
</html>
TestAction:
package com.zgxr.struts;
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.actions.LookupDispatchAction;
public class TestAction extends LookupDispatchAction {
@Override
//用于关联按键和方法
protected Map getKeyMethodMap() {
Map map = new HashMap();
//如果按钮标题的key为button.sum. 则提交该按钮时对应sum方法
map.put( "button.sum", "sum");
//如果按钮标题的key为button.minus. 则提交该按钮时对应minus方法
map.put( "button.minus", "minus");
//如果按钮标题的key为button.multiply. 则提交该按钮时对应multiply方法
map.put( "button.multiply", "multiply");
//如果按钮标题的key为button.divide. 则提交该按钮时对应divide方法
map.put( "button.divide", "divide");
return map;
}
public ActionForward sum(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算加法
int num = loginActionForm.getNumOne() + loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward minus(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算减法
int num = loginActionForm.getNumOne() - loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward multiply(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算乘法
int num = loginActionForm.getNumOne() * loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward divide(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算除法
int num = loginActionForm.getNumOne() / loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
}
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.actions.LookupDispatchAction;
public class TestAction extends LookupDispatchAction {
@Override
//用于关联按键和方法
protected Map getKeyMethodMap() {
Map map = new HashMap();
//如果按钮标题的key为button.sum. 则提交该按钮时对应sum方法
map.put( "button.sum", "sum");
//如果按钮标题的key为button.minus. 则提交该按钮时对应minus方法
map.put( "button.minus", "minus");
//如果按钮标题的key为button.multiply. 则提交该按钮时对应multiply方法
map.put( "button.multiply", "multiply");
//如果按钮标题的key为button.divide. 则提交该按钮时对应divide方法
map.put( "button.divide", "divide");
return map;
}
public ActionForward sum(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算加法
int num = loginActionForm.getNumOne() + loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward minus(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算减法
int num = loginActionForm.getNumOne() - loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward multiply(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算乘法
int num = loginActionForm.getNumOne() * loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
public ActionForward divide(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//将form转变成提交时用到的loginActonForm
LoginActionForm loginActionForm = (LoginActionForm) form;
//计算除法
int num = loginActionForm.getNumOne() / loginActionForm.getNumTwo();
request.setAttribute( "action", num);
return mapping.findForward( "success");
}
}
ApplicationResources.properties
button.sum=sum
button.minus=minus
button.multiply=multiply
button.divide=divide
button.minus=minus
button.multiply=multiply
button.divide=divide
好了就这么多了哦忘了web.xml了
web.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< servlet >
< servlet-name >action </ servlet-name >
< servlet-class >
org.apache.struts.action.ActionServlet
</ servlet-class >
< init-param >
< param-name >application </ param-name >
< param-value >ApplicationResources </ param-value >
</ init-param >
< init-param >
< param-name >config </ param-name >
< param-value >/WEB-INF/struts-config.xml </ param-value >
</ init-param >
< init-param >
< param-name >debug </ param-name >
< param-value >3 </ param-value >
</ init-param >
< init-param >
< param-name >detail </ param-name >
< param-value >3 </ param-value >
</ init-param >
< load-on-startup >0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >action </ servlet-name >
< url-pattern >*.do </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file >/UserManagement.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
< servlet >
< servlet-name >action </ servlet-name >
< servlet-class >
org.apache.struts.action.ActionServlet
</ servlet-class >
< init-param >
< param-name >application </ param-name >
< param-value >ApplicationResources </ param-value >
</ init-param >
< init-param >
< param-name >config </ param-name >
< param-value >/WEB-INF/struts-config.xml </ param-value >
</ init-param >
< init-param >
< param-name >debug </ param-name >
< param-value >3 </ param-value >
</ init-param >
< init-param >
< param-name >detail </ param-name >
< param-value >3 </ param-value >
</ init-param >
< load-on-startup >0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >action </ servlet-name >
< url-pattern >*.do </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file >/UserManagement.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
本文出自 “乔磊的博客 学习 进步” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/360059