激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - struts1實(shí)現(xiàn)簡(jiǎn)單的登錄功能實(shí)例(附源碼)

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

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

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

環(huán)境:myeclipse 14     

1   struts1 框架搭建

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

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

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

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

點(diǎn)擊next

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

選擇*.do ,改下包名改成與你項(xiàng)目相關(guān)的。如我的包名為com.lichang.struts1

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

點(diǎn)擊next

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

點(diǎn)擊完成,在我們的web-inf下就會(huì)多出struts-config.xml文件

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

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

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

2 接著我們就開(kāi)始編程來(lái)實(shí)現(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:指定訪問(wèn)時(shí)的路徑  type:指定action所在的類(一般是:包名.類名) name:指定和哪個(gè)表單(和jsp中javabean
  差不多的東西)對(duì)應(yīng),該例中name就和com.lichang.struts1.loginactionform類對(duì)應(yīng)-->
    <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;
//這個(gè)類充當(dāng)控制器
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è)務(wù)邏輯類
    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", "密碼錯(cuò)誤");
    }
    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ù),相當(dāng)于jsp中的javabean)
 * 不過(guò)sturts1在底層上實(shí)現(xiàn)了一些特別的方法以至于當(dāng)java程序員定義了javabean并繼承actionform并實(shí)現(xiàn)setxxx()方法時(shí)
 * 用戶表單中元素的值就被一一賦給我們自己定義的變量。如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;
//這個(gè)類就是用來(lái)處理用戶登錄的業(yè)務(wù)邏輯
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
  }
 
}

運(yùn)行部分截圖

登錄界面

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

用戶名不存在

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

源代碼下載地址:struts1_login.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费亚洲视频在线观看 | 欧美日韩精品中文字幕 | 亚洲天堂在线电影 | 一色桃子av大全在线播放 | 精品中文字幕视频 | 亚洲99影视一区二区三区 | 欧美人xx | 欧美精品网址 | 久久久国产一级片 | 双性精h调教灌尿打屁股的文案 | 香蕉久久久久久 | 91国内精品久久久久免费影院 | 国产成人强伦免费视频网站 | 北京一级毛片 | 欧洲a级片 | 欧美成人影院 | 久草在线观看资源 | 欧美91看片特黄aaaa | 成人啪啪18免费网站 | 国产乱淫a∨片免费观看 | 欧美一级二级毛片视频 | 最新欧美精品一区二区三区 | 中文字幕在线观看成人 | 亚洲天堂岛国片 | 一级黄色片在线看 | 91成人免费在线观看 | 国产精品视频在线免费观看 | 日韩精品一区二区在线 | 日本特级a一片免费观看 | 久久久精品网 | 成人三级电影在线 | 一级免费看片 | 免费人成年短视频在线观看网站 | 手机国产乱子伦精品视频 | 日韩av成人| 91av在线免费| 欧美精品国产综合久久 | 久久网页| 国产精品影视 | 日本人乱人乱亲乱色视频观看 | 91美女视频在线观看 |