1首先创建项目,导入jar包,结构如图
2配置web.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> </web-app>
3.配置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:类路径 --> <form-bean name="loginForm" type="com.trustview.form.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.trustview.action.Login" 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>
4.action代码
package com.trustview.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.trustview.form.LoginForm; public class Login 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 = 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"); } private String login(String username, String password) { if("admin".equals(username)&&"admin".equals(password)){ return "success"; }else if(!"admin".equals(username)){ return "password"; } return "username"; } }
5.form代码
package com.trustview.form; 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; } }
index.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> 请输入用户名密码 <head> <title>首页</title> </head> <body> <html:form action="/login.do"> 用户名:<html:text property="username"></html:text><br/> 密码: <html:password property="password"></html:password><br/> <html:submit>提交</html:submit> </html:form> </body> </html>
时间: 2024-10-17 21:41:30