<servlet>
<servlet-name>action</servlet-name>
<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>
<servlet> <servlet-name>action</servlet-name> <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>
<struts-config>
<form-beans>
<form-beanname="loginForm"type="com.wang.struts.LoginActionForm"></form-bean>
<!-- 可以有多个ActionForm -->
</form-beans>
<action-mappings>
<actionpath="/login"type="com.wang.struts.LoginAction"name="loginForm"scope="request">
<forwardname="success"path="/login_success.jsp"></forward>
<forwardname="error"path="/login_error.jsp"></forward>
</action>
<!-- 可以有多个Action -->
</action-mappings>
</struts-config>
<struts-config> <form-beans> <form-bean name="loginForm" type="com.wang.struts.LoginActionForm"></form-bean> <!-- 可以有多个ActionForm --> </form-beans> <action-mappings> <action path="/login" type="com.wang.struts.LoginAction" name="loginForm" scope="request"> <forward name="success" path="/login_success.jsp"></forward> <forward name="error" path="/login_error.jsp"></forward> </action> <!-- 可以有多个Action --> </action-mappings> </struts-config>
6. 必须继承Action的类。代码示例:
package com.wang.struts;
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;
publicclass LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LoginActionForm loginActionForm = (LoginActionForm)form;
String username = loginActionForm.getUsername();
String password = loginActionForm.getPassword();
if ("admin".equals(username) && "admin".equals(password)) {
return mapping.findForward("success");
} else {
return mapping.findForward("error");
}
}
}
package com.wang.struts; 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 { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginActionForm loginActionForm = (LoginActionForm)form; String username = loginActionForm.getUsername(); String password = loginActionForm.getPassword(); if ("admin".equals(username) && "admin".equals(password)) { return mapping.findForward("success"); } else { return mapping.findForward("error"); } } }
7. 必须继承ActionForm的类。示例代码:
package com.wang.struts;
import org.apache.struts.action.ActionForm;
publicclass LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
publicvoid setPassword(String password) {
this.password = password;
}
}
package com.wang.struts; import org.apache.struts.action.ActionForm; public class LoginActionForm extends ActionForm { private String username; private String password; 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; } }