Home  >  Article  >  Java  >  Code example for struts1 to implement login function

Code example for struts1 to implement login function

Y2J
Y2JOriginal
2017-04-28 09:51:241430browse

This article mainly introduces an example of simple login function implemented by struts1 (source code attached). The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look.

Environment: MyEclipse 14

1 struts1 framework construction

Create a new web project in MyEclipse and name it struts1_login. At this time, it is an empty document and I will not take a screenshot. Then right-click on the project->select myeclipse->add struts capabilities

Click Install Apache Struts (1. x)Facet

Click next

Select *.do and change the package name to something related to your project of. For example, my package name is com.lichang.struts1

. Click next

and click Finish on our WEB There will be an additional struts-config.xml file under -INF

The above is the general process of letting myeclipse help us add the framework. The overall structure of the project is as follows:

At this point, our struts1 framework has been built 2 and then we started programming to implement it.

2 Then we started programming to implement it.

web.xml is as follows:

<?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>

Then configure LoginAction code in struts.xml as follows:

<?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>

index.jsp code is as follows:

<%@ 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>

login_error.jsp code is as follows:

<%@ 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>

login_success.jsp code is as follows:

<%@ 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>

LoginAction.java code is as follows:

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");
  }
}

LoginActionForm.java code as follows:

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;
  }
}

UserManager.java code is as follows:

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 code is as follows:

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 code is as follows:

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
  }

}

Run part screenshot

Login interface

Username does not exist

##Source code download address: struts1_login_jb51.rar

The above is the detailed content of Code example for struts1 to implement login function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn