一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - struts1實現(xiàn)簡單的登錄功能實例(附源碼)

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

2020-09-15 14:22sizai Java教程

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

環(huán)境:myeclipse 14     

1   struts1 框架搭建

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

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

單擊上面install apache struts(1.x)facet

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

點擊next

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

選擇*.do ,改下包名改成與你項目相關的。如我的包名為com.lichang.struts1

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

點擊next

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

點擊完成,在我們的web-inf下就會多出struts-config.xml文件

以上就是讓myeclipse幫我們加入框架的大概過程。項目的整體結(jié)構(gòu)如下:

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

至此我們的struts1 框架搭建完成2 接著我們就開始編程來實現(xiàn)了。

2 接著我們就開始編程來實現(xiàn)了。

 web.xml 如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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 代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ 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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ 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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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();
    //調(diào)用業(yè)務邏輯類
    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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.lichang.struts1;
 
import org.apache.struts.action.actionform;
 
/**
 *
 * actionform(表單用于獲取用戶輸入的數(shù)據(jù),相當于jsp中的javabean)
 * 不過sturts1在底層上實現(xiàn)了一些特別的方法以至于當java程序員定義了javabean并繼承actionform并實現(xiàn)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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.lichang.struts1;
//這個類就是用來處理用戶登錄的業(yè)務邏輯
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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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實現(xiàn)簡單的登錄功能實例(附源碼)

用戶名不存在

struts1實現(xiàn)簡單的登錄功能實例(附源碼)

源代碼下載地址:struts1_login.rar

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜神器18以下不能进免费 | 网红思瑞一区二区三区 | 欧美日韩亚洲高清不卡一区二区三区 | 91短视频在线免费观看 | 国产成人精品日本亚洲网址 | 国产51 | 成人国产精品视频 | 午夜福利视频极品国产83 | 国产精品一区牛牛影视 | 四虎网站最新网址 | 天天干天天操天天爽 | 免费视频亚洲 | 欧美成人中文字幕 | 青青国产在线视频 | 国产精品欧美一区二区 | 国产在线视频自拍 | 亚洲精品午夜视频 | 日本五十路六十30人8时间 | 黑人好大 | 性欧美sexovideotv | 海绵宝宝第二季全集免费观看 | china外卖员gay帮口 | 国语刺激对白勾搭视频在线观看 | 美女扒开屁股让男人进去 | 久草高清在线 | 丝瓜污污视频 | 被巨大黑人的翻白眼 | 欧美黑人一级 | 人人爽人人香蕉 | 亚洲国产成人精品无码区5566 | 天天综合色天天综合 | 成年人免费在线看的惊悚动作片 | v视界影院成片 | 美女又爽又黄免费 | 日本黄a| 久久黄色小视频 | 99国内精品 | 亚洲福利一区二区三区 | 男人天堂999 | 欧美xxx000喷水 | 国产精品全国探花在线观看 |