一:首页要在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;
}
}