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

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

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

服務器之家 - 編程語言 - Java教程 - struts1登錄示例代碼_動力節點Java學院整理

struts1登錄示例代碼_動力節點Java學院整理

2020-12-24 11:48lfsf802 Java教程

這篇文章主要介紹了struts1登錄示例代碼,需要的朋友可以參考下

struts1框架實例—登錄實例:

1、實例開始工作—導入jar包,在官網上下載struts1框架包,解壓之后導入工程的:

struts1登錄示例代碼_動力節點Java學院整理

      2、之后配置web.xml(這里的具體配置方法可以參見struts1框架包中的實例文件夾webapps中的實例代碼中web.xml文件的配置方法):  

struts1登錄示例代碼_動力節點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
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <welcome-file-list>
 <welcome-file>index.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>2</param-value>
 </init-param>
 <init-param>
  <param-name>detail</param-name>
  <param-value>2</param-value>
 </init-param>
 <load-on-startup>2</load-on-startup>
 </servlet>
 <!-- standard action servlet mapping -->
 <servlet-mapping>
 <servlet-name>action</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app></span>

        首先這個配置文件中最主要的就是做了兩件的事情,一個是配置actionservlet,一個是初始化struts-config.xml配置文件參數。 

       3、配置完了web.xml文件,之后我們就要開始進入項目代碼階段了。

       登錄頁面:      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contenttype="text/html; charset=gb18030"
 pageencoding="gb18030"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb18030">
<title>insert title here</title>
</head>
<body>
 <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>

       切記那個action后面的路徑一定要是.do開頭的,因為我們在web.xml中配置的是*.do。這里依舊不介紹為什么隨后博客會深入分析。

      4、建立兩個異常類,一個是用戶名未找到、一個是密碼錯誤:

      ①用戶名未找到

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
 }
}

      ②密碼錯誤 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
 }
}

        5、業務處理類代碼:

?
1
2
3
4
5
6
7
8
9
10
public class usermanager {
 public void login(string username, string password) {
  if (!"admin".equals(username)) {
   throw new usernotfoundexception();
  
  if (!"admin".equals(password)) {
   throw new passworderrorexception();
  }  
 }
}

       6、建立loginactionform類,這個類繼承actionform類,簡單說一下這個類,這個類主要是負責收集表單數據的,在這里一定要注意表單的屬性必須和actionform中的get和set方法的屬性一致。這里依舊不深入解釋,隨后博客都會涉及到。       

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.apache.struts.action.actionform;
/**
 * 登錄actionform,負責表單收集數據
 * 表單的屬性必須和actionform中的get和set的屬性一致
 * @author administrator
 *
 */
@suppresswarnings("serial")
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;
 
}

       7、loginaction類的建立,這個是負責取得表單數據、調用業務邏輯以及返回轉向信息。        

?
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
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;
/**
 * 登錄action
 * 負責取得表單數據、調用業務邏輯、返回轉向信息
 *
 * @author administrator
 *
 */
public class loginaction extends action {
 @override
 public actionforward execute(actionmapping mapping, actionform form,
   httpservletrequest request, httpservletresponse response)
   throws exception {
  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");
  }
}

      8、既然有轉向,那么我們還要建立兩個頁面,一個是登錄成功頁面,一個登錄失敗頁面。

           ①登錄成功頁面           

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contenttype="text/html; charset=gb18030"
 pageencoding="gb18030"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb18030">
<title>insert title here</title>
</head>
<body>
 ${loginform.username },登錄成功
</body>
</html>

           ②登錄失敗頁面            

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contenttype="text/html; charset=gb18030"
 pageencoding="gb18030"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb18030">
<title>insert title here</title>
</head>
<body>
 <%--
 <%=request.getattribute("msg") %>
 --%>
 ${msg }
</body>
</html>

       9、最后要進行struts-config.xml的配置。
        
<?xml version="1.0" encoding="iso-8859-1" ?>  
<!doctype struts-config public  
          "-//apache software foundation//dtd struts configuration 1.2//en"  
          "">  
<struts-config>  
    <form-beans>  
        <form-bean name="loginform" type="com.bjpowernode.struts.loginactionform"/>  
    </form-beans>  
    <action-mappings>  
        <action path="/login"   
                type="com.bjpowernode.struts.loginaction"  
                name="loginform"          
                scope="request"       
                >  
            <forward name="success" path="/login_success.jsp" />  
            <forward name="error" path="/login_error.jsp"/>         
        </action>  
    </action-mappings>  
</struts-config>

       經過配置之后實例就已經做完了,感興趣童鞋可以自己手動運行一下。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99久久九九爱看免费直播 | 成人在线第一页 | 中文字幕线观看 | 97人人草 | 久久综合精品视频 | 国内精品久久久久久久影视红豆 | 免费网站看v片在线a | 欧美成人一级片 | 国产精品久久久久久久四虎电影 | 免费的毛片 | 精品亚洲一区二区 | 国产黄色网 | 欧美一级黄 | 91成人一区二区三区 | av色在线观看 | 最新一区二区三区 | 一区在线免费视频 | 草逼一区 | 一级片在线观看 | 精品一区二区三区日本 | 日本看片一区二区三区高清 | 久久精品一区二区三区四区五区 | 国产精品久久久久久久不卡 | 亚洲人片在线观看 | 国产精品久久久不卡 | 天天夜干| 欧美天堂一区 | 亚洲国产成人一区 | 亚洲成人在线免费观看 | 精品成人一区二区三区 | 新久草视频 | 九九视频在线观看6 | 欧美a一| 日韩视频不卡 | 91短视频在线视频 | 国产精品视频一区二区三区四 | 亚洲成人伊人 | 摸逼逼视频 | 欧美性生交大片 | 毛片小网站 | 日本一区二区在线 |