标签:
<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>
5. 配置struts-config.xml部署Action、ActionForm。其中type均为你项目中Action和ActionForm所在包的完整路径;name可以随便起,但是Action中的name必须与它用到的ActionForm的name相一致;再有就是action中要添加scope="request"这个属性,因为scope默认是session,这个可以根据实际情况自己定。
<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; 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; 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; } }
标签:
原文地址:http://www.cnblogs.com/wzsblogs/p/4458154.html