1. input.jsp: 只是提供两本基本的按键:update和delete,当然用户可以增加其它的按键,然后在Action加入其它值的
public ActionForward otherValue(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) { }
比如你加入了一个<html:submit property=“method“ value=“addOrder“/>
那么就要在DispatchAction中加入
public ActionForward addOrder(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
/*
* 其它的一些代码
*/
return mapping.findForward("delete");
}
input.jsp的代码:<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<body>
<html:form action="/userManipulcation">
name : <html:text property="name"/><html:errors property="name"/></br>
address : <html:text property="address"/><html:errors property="address"/></br>
<html:submit property="method" value="update"/>
<html:submit property="method" value="delete"/>
</html:form>
</body>
</html>
2. update.jsp: 当用户按update键在input.jsp中,最后转到update.jsp
<html>
<body>
This is the Update Page<br>
</body>
</html>
3. delete.jsp: 当用户按delete键在input.jsp中,最后转到delete.jsp
<html>
<body>
This is the Delete Page<br>
</body>
</html>
4. 我的DispatchAction:如下
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.0/xslt/JavaClass.xsl
package com.yourcompany.struts.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.actions.DispatchAction;
import com.yourcompany.struts.form.UserManipulcationForm;
/**
* MyEclipse Struts
* Creation date: 08-23-2004
*
* XDoclet definition:
* @struts:action path="/userManipulcation" name="userManipulcationForm" input="/form/userManipulcation.jsp" parameter="type" scope="request"
*/
public class UserManipulcationAction extends DispatchAction {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward update(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
/*
* 其它的一些代码对应其按update键
*/
return mapping.findForward("update");
}
public ActionForward delete(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
UserManipulcationForm userManipulcationForm = (UserManipulcationForm) form;
/*
* 其它的一些代码对应其按delete键
*/
return mapping.findForward("delete");
}
}
要注意得就是,对就按的键中的value不同,DispatchAction用自动执行其它想对应value的方法。。
其它的:
ActionForm:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.0/xslt/JavaClass.xsl
package com.yourcompany.struts.form;
import org.apache.struts.action.ActionForm;
/**
* MyEclipse Struts
* Creation date: 08-23-2004
*
* XDoclet definition:
* @struts:form name="userManipulcationForm"
*/
public class UserManipulcationForm extends ActionForm {
// --------------------------------------------------------- Instance Variables
/** address property */
private String address;
/** name property */
private String name;
/** method property */
private String method;
// --------------------------------------------------------- Methods
/**
* Returns the address.
* @return String
*/
public String getAddress() {
return address;
}
/**
* Set the address.
* @param address The address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}
/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
}
Struts-Config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userManipulcationForm" type="com.yourcompany.struts.form.UserManipulcationForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userManipulcationForm"
input="/form/userManipulcation.jsp"
name="userManipulcationForm"
parameter="method"
path="/userManipulcation"
scope="request"
type="com.yourcompany.struts.action.UserManipulcationAction"
validate="false" >
<forward name="update" path="/update.jsp" />
<forward name="delete" path="/delete.jsp" />
</action>
</action-mappings>
<controller bufferSize="4096" debug="0" />
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
要注意的就是。。
DispatchAction是通过在struts-config.xml中定义的 parameter="method"
来区别不同的method的值执行不同的方法。。。
就<html:submit property=”method” value=”update”/>
博客介绍了Struts框架中DispatchAction的使用,通过input.jsp提供update和delete等按键,根据按键value值执行不同方法。还给出了update.jsp、delete.jsp页面代码,以及DispatchAction、ActionForm和Struts-Config.xml的代码示例,强调DispatchAction通过parameter区分方法。
1193

被折叠的 条评论
为什么被折叠?



