Maison >Java >javaDidacticiel >Exemple de code pour struts1 pour implémenter la fonction de connexion
Cet article présente principalement un exemple de fonction de connexion simple implémentée par struts1 (code source ci-joint). L'éditeur pense que c'est plutôt bon, je vais donc le partager avec vous maintenant et le donner comme référence. Suivons l'éditeur pour jeter un œil
Environnement : MyEclipse 14
Construction du framework 1 struts1
Créez un nouveau projet Web dans MyEclipse et nommez-le struts1_login Pour le moment, c'est un document vide et je ne prendrai pas de capture d'écran. Ensuite, faites un clic droit sur le projet->sélectionner myeclipse->ajouter des capacités struts
2 Ensuite, nous avons commencé la programmation pour l'implémenter.
web.xml est le suivant :<?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>Ensuite, configurez le code LoginAction dans struts.xml comme suit :
<?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>le code index.jsp est comme suit :
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!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" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </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>le code login_error.jsp est le suivant :
<%@ 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" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <%-- <%=request.getAttribute("msg") %> --%> ${msg } </body> </html>le code login_success.jsp est le suivant :
<%@ 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" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> ${username },登录成功 </body> </html>Code LoginAction.java est le suivant :
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"); } }Le code LoginActionForm.java est le suivant :
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; } }Le code UserManager.java est le suivant :
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(); } } }UserNotFoundException.java le code est le suivant :
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 } }PasswordErrorException.java Le code est le suivant :
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 } }Capture d'écran de la partie en cours d'exécutionInterface de connexion
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!