序:struts 1.x的dispatch action,是一种分发的处理,当一个画面上,你想要提交一个action,但是这个action要做不同的处理的时候,可以考虑使用。其中,个人认为最关键的有以下几点:
1.jsp中请求的action的url:"./dispatch.do?action=search"
2.struts-config.xml中的parameter参数,一定要写成你提交的method的名称
1. jsp:
要注意dispatch和action,dispatch要写到struts-config.xml的action path 下,action要
写到action path 的parameter的属性下
<%@ page language="java" contentType="text/html; charset=windows-31j"
pageEncoding="windows-31j"%>
<%@ taglib uri="/struts-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
</head>
<body>
<html:link href="./dispatch.do?action=search">search</html:link><br>
<html:link href="./dispatch.do?action=logout">logout</html:link>
</body>
</html>
2.struts-config.xml:
<action name="InputForm" path="/dispatch"
type="sources.dispatchAction"
parameter="action"
input="/pages/logonSucess.jsp"
scope="session">
<forward name="success" path="/searchSCR.do"></forward>
<forward name="success1" path="/loginSCR.do"></forward>
</action>
3.action:
/**
*
*/
package sources;
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;
/**
* @author liusk
*
*/
public class dispatchAction extends DispatchAction{
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// 自動生成されたメソッド・スタブ
InputForm userForm = (InputForm) form;
String userName = userForm.getUserName();
//String password = userForm.getPassword();
if ("".equals(userName) || userName == null) {
return mapping.findForward("failure");
} else {
return mapping.findForward("success");
}
}
public ActionForward logout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// 自動生成されたメソッド・スタブ
InputForm userForm = (InputForm) form;
String userName = userForm.getUserName();
//String password = userForm.getPassword();
if ("".equals(userName) || userName == null) {
return mapping.findForward("failure");
} else {
return mapping.findForward("success1");
}
}
}
4,actionForm:
/**
*
*/
package sources;
import org.apache.struts.validator.ValidatorActionForm;
/**
* @author liusk
*
*/
public class InputForm extends ValidatorActionForm{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -4309037220475269906L;
private String userName;
private String password;
/**
* @return password
*/
public String getPassword() {
return password;
}
/**
* @param password 設定する password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName 設定する userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
}