1、简介
struts是WEB程序MVC分层架构中的C,属于控制层,主要进行处理用户的请求,基于请求驱动。获取用户的请求地址并将表单中的数据封装到Form对象后交给Action进行处理。在Action中进行条用业务层处理具体的请求后将结果通过ActionMapping封装跳转地址返回给用户。struts是对servlet的再次封装,使得更加灵活高效。下面以一个登录的实例讲解struts的开发过程。
2、在新建的web工程中添加以下struts1.x jar包
antlr-2.7.6.jar、commons-beanutils.jar、commons-digester.jar、commons-fileupload.jar、commons-logging-1.0.4.jar、commons-validator.jar、jakarta-oro.jar、struts.jar。
3、web.xml进行如下配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>MyStruts1Prj</display-name> <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/conf/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4、新建登陆页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <% String basePath = request.getContextPath(); %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆页面</title> </head> <body> <h1>登陆页面</h1> <hr> <form action="<%=basePath %>/login.do" method="post" > userName:<input id="userName" name="userName" type="text" /><br> passWord:<input id="passWord" name="passWord" type="password" /><br> <input type="submit" id="submit" name="submit" value="submit" /> </form> </body> </html>
5、新建登陆成功后的跳转页面loginSucces.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆成功页面</title> </head> <body> <h1>欢迎[<%=request.getAttribute("userName") %>]登陆成功!</h1> </body> </html>
6、新建登陆失败后的跳转页面loginError.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登陆失败页面</title> </head> <body> <h1>登陆失败!</h1> </body> </html>
7、新建LoginActionForm.java
package com.lanp.webapp.form; import org.apache.struts.action.ActionForm; /** * 封装登陆表单数据的FORM类 * @author LanP * @since 2012年4月11日23:07:09 * @version v1.0 */ @SuppressWarnings("serial") 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; } }
8、新建LoginAction.java
package com.lanp.webapp.action; 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; import com.lanp.webapp.form.LoginActionForm; /** * 处理登陆的Action类 * @author LanP * @since 2012年4月11日23:07:09 * @version v1.0 */ public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = "error"; LoginActionForm loginActionForm = (LoginActionForm)form; String userName = loginActionForm.getUserName(); String passWord = loginActionForm.getPassWord(); if(null != userName && "admin".equals(userName) && null != passWord && "admin".equals(passWord)) { path = "success"; request.setAttribute("userName", userName); } else { path = "error"; } return mapping.findForward(path); } }
9、配置struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginActionForm" type="com.lanp.webapp.form.LoginActionForm"> </form-bean> </form-beans> <action-mappings> <action path="/login" type="com.lanp.webapp.action.LoginAction" name="loginActionForm" scope="request"> <forward name="success" path="/jsp/loginSucces.jsp" /> <forward name="error" path="/jsp/loginError.jsp" /> </action> </action-mappings> </struts-config>
OK,TKS!