一:首頁要在web.xml中添加相應的struts配置:
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<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>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
二:然後在config所對應的目錄下添加struts配置檔案struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ActionForm -->
<form-beans>
<!-- name:名稱 type:類路徑 配置bean-->
<form-bean name="loginForm" type="com.login.LoginForm">
</form-bean>
</form-beans>
<!-- 設定全局URL,必須寫在Action前面,這樣任何Action都可以使用 -->
<global-forwards>
<forward name="result" path="/result.jsp" />
</global-forwards>
<!-- 注冊Action -->
<action-mappings>
<!-- name:名稱 type:類路徑 path:用戶端(JSP)送出到伺服器端時指定的路徑(Form表單中的action=”/login.do" -->
<action path="/login" type="com.login.LoginAction"
name="loginForm" scope="request">
<!-- 局部URL,隻有這個action可以使用 -->
<forward name="success" path="/success.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
</struts-config>
三:
注:Action和Form的關系是一個Action隻能對應一個Form(通過Action的name屬性)
而一個Form可以被多個Action調用
LoginAction.java
package com.login;
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;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm lf = (LoginForm) form;
String username = lf.getUsername();
String password = lf.getPassword();
String errorInfo = "";
String result = UserManager.getInstance().login(username, password);
if (result.equals("success")) {
request.setAttribute("username", username);
return mapping.findForward("success");
} else if (result.equals("username")) {
errorInfo = "密碼錯誤";
} else {
errorInfo = "使用者名錯誤";
}
request.setAttribute("errorInfo", errorInfo);
return mapping.findForward("error");
}
}
四:LoginForm.java
package com.login;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginForm extends ActionForm {
private String username;
private String password;
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("--------reset()-------------");
super.reset(mapping, request);
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("--------validate()-------------");
return super.validate(mapping, request);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}