首頁  >  文章  >  Java  >  struts1實作登入功能的程式碼實例

struts1實作登入功能的程式碼實例

Y2J
Y2J原創
2017-04-28 09:51:241428瀏覽

本篇文章主要介紹了struts1實作簡單的登入功能實例(附源碼),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧

環境:MyEclipse 14     

1   struts1 框架建構

在MyEclipseweb project 取名為struts1_login,此時是一個空文檔就不截圖了然後在project上右鍵->選擇myeclipse->add struts capabilities

##點擊上面Install Apache Struts(1. x)Facet

點選next

#選擇*.do ,改為與你專案相關的。如我的套件名為com.lichang.struts1

點擊next

點擊完成,在我們的WEB -INF下就會多出struts-config.xml檔

以上就是讓myeclipse幫我們加入框架的大概流程。專案的整體架構如下:

至此我們的struts1 框架建置完成2 接著我們就開始程式設計來實現了。

2 接著我們就開始程式設計來實現了。

 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>

然後在struts.xml設定LoginAction 程式碼如下:

<?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程式碼如下:

<%@ 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程式碼如下:

<%@ 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程式碼如下:

<%@ 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程式碼如下:

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程式碼如下:

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程式碼如下:

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程式碼如下:

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程式碼如下:

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
  }

}

執行部分截圖

登入介面

使用者名稱不存在

#原始碼下載位址:struts1_login_jb51.rar

#

以上是struts1實作登入功能的程式碼實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn