注意:如下兩個例子在調試的過程當中用的是同一個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>
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");
}
}
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;
}
}
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
>
好了把以上這些拷下來試試吧,下面咱們來看看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>
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");
}
}
ApplicationResources.properties
button.sum=sum
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
>
你們要注意了在不用struts標籤的時候,form提交的路徑要注意具體見以上代碼