環境:MyEclipse 14 發佈時間: 2017-04-20 21:04:55css
在MyEclipse新建web project 取名爲struts1_login,此時是一個空文檔就不截圖了而後在project上右鍵->選擇myeclipse->add struts capabilitieshtml
單擊上面Install Apache Struts(1.x)Facetjava
點擊next程序員
選擇*.do ,改下包名改爲與你項目相關的。如個人包名爲com.lichang.struts1web
點擊nextapache
點擊完成,在咱們的WEB-INF下就會多出struts-config.xml文件編程
以上就是讓myeclipse幫咱們加入框架的大概過程。項目的總體結構以下:app
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts1_login</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.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>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> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.lichang.struts1.LoginActionForm"/> </form-beans> <action-mappings> <!-- path:指定訪問時的路徑 type:指定Action所在的類(通常是:包名.類名) name:指定和哪一個表單(和jsp中Javabean 差很少的東西)對應,該例中name就和com.lichang.struts1.LoginActionForm類對應--> <action path="/login" type="com.lichang.struts1.LoginAction" name="loginForm" scope="request" > <forward name="success" path="/login_success.jsp" /> <forward name="error" path="/login_error.jsp"/> </action> </action-mappings> <message-resources parameter="com.lichang.struts1.ApplicationResources" /> </struts-config>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>eclipse
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html style="text-align:center">
<head style="text-align:center">
<title>index page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body style="text-align:center">
<form action="login.do" method="post">
用戶:<input type="text" name="username"><br>
密碼:<input type="password" name="password"></br>
<input type="submit" value="登陸">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>error page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>success page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${username },登陸成功 </body> </html>
package com.lichang.struts1; 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 { //從actionForm中獲取username和password LoginActionForm laf = (LoginActionForm)form; String username = laf.getUsername(); String password = laf.getPassword(); //調用業務邏輯類 UserManager userManager = new UserManager(); try { userManager.login(username, password); return mapping.findForward("success"); }catch(UserNotFoundException e) { e.printStackTrace(); request.setAttribute("msg", "用戶不能找到,用戶名稱=" + username ); }catch(PasswordErrorException e) { e.printStackTrace(); request.setAttribute("msg", "密碼錯誤"); } return mapping.findForward("error"); } }
package com.lichang.struts1; import org.apache.struts.action.ActionForm; /** * * ActionForm(表單用於獲取用戶輸入的數據,至關於jsp中的Javabean) * 不過sturts1在底層上實現了一些特別的方法以致於當Java程序員定義了Javabean並繼承ActionForm並實現setXXX()方法時 * 用戶表單中元素的值就被一一賦給咱們本身定義的變量。如public void setUsername(String username)方法就可把form中username * 賦給username變量 * */ 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; } }
package com.lichang.struts1; //這個類就是用來處理用戶登陸的業務邏輯 public class UserManager { public void login(String username, String password) { if (!"admin".equals(username)) { throw new UserNotFoundException(); } if (!"admin".equals(password)) { throw new PasswordErrorException(); } } }
package com.lichang.struts1; public class UserNotFoundException extends RuntimeException { public UserNotFoundException() { // TODO Auto-generated constructor stub } public UserNotFoundException(String message) { super(message); // TODO Auto-generated constructor stub } public UserNotFoundException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public UserNotFoundException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } }
package com.lichang.struts1; public class PasswordErrorException extends RuntimeException { public PasswordErrorException() { // TODO Auto-generated constructor stub } public PasswordErrorException(String message) { super(message); // TODO Auto-generated constructor stub } public PasswordErrorException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } public PasswordErrorException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } }
用戶名不存在