具体步骤:
1、导入jar包
本例子基于一个基本的登录功能,只用到commons-beanutils.jar、commons-digester.jar、commons-logging.jar、struts.jar这四个jar包。
2、修改web.xml文件
在该文件增加<servlet><servlet/>和<servlet-mapping><servlet-mapping/>节点
3、创建和配置struts-config.xml
该文件和web.xml放在同一目录。
4、编写实现类和jsp页面
具体代码如下:
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>Struts1</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/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
LoginFormLoginFormLoginForm类:用于封装页面输入的表单信息
package form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class LoginForm extends ActionForm { private String userId; private String userName; private String userPwd; //恢复属性的初始状态 public void reset(ActionMapping mapping, HttpServletRequest request) { this.userId = null; this.userName = null; this.userPwd = null; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
LoginActionLoginActionLoginAction类:继承自org.apache.struts.action
package 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 form.LoginForm; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm = (LoginForm)form; if(!loginForm.getUserName().equals("") && loginForm.getUserName().equals(loginForm.getUserPwd())){ request.getSession().setAttribute("userName", loginForm.getUserName()); return mapping.findForward("index"); } return mapping.findForward("login"); } }
struts-config.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?> <!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="loginForm" type="form.LoginForm"></form-bean> </form-beans> <action-mappings> <action path="/login" name="loginForm" type="action.LoginAction"> <forward name="index" path="/index.jsp" redirect="true"></forward> <forward name="login" path="/login.jsp"></forward> </action> </action-mappings> </struts-config>
登录页面 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页面</title> </head> <body> <form action="/Lesson11_Struts1Base/login.do" method="post"> 用户名:<input type="text" name="userName"><br> 密码:<input type="password" name="userPwd"><br> <input type="submit" value="提交"> </form> </body> </html>
首页index.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> 欢迎你,${userName } </body> </html>
到此,一个struts例子基本完成。
更多资源: http://www.cnblogs.cn/jdbc/archive/2012/08/08/JA.html