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

服務(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登錄示例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

struts1登錄示例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

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

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

struts1框架實(shí)例—登錄實(shí)例:

1、實(shí)例開(kāi)始工作—導(dǎo)入jar包,在官網(wǎng)上下載struts1框架包,解壓之后導(dǎo)入工程的:

struts1登錄示例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

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

struts1登錄示例代碼_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理    

     具體如下:     

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

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

       3、配置完了web.xml文件,之后我們就要開(kāi)始進(jìn)入項(xiàng)目代碼階段了。

       登錄頁(yè)面:      

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

       切記那個(gè)action后面的路徑一定要是.do開(kāi)頭的,因?yàn)槲覀冊(cè)趙eb.xml中配置的是*.do。這里依舊不介紹為什么隨后博客會(huì)深入分析。

      4、建立兩個(gè)異常類,一個(gè)是用戶名未找到、一個(gè)是密碼錯(cuò)誤:

      ①用戶名未找到

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

      ②密碼錯(cuò)誤 

?
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、業(yè)務(wù)處理類代碼:

?
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類,這個(gè)類繼承actionform類,簡(jiǎn)單說(shuō)一下這個(gè)類,這個(gè)類主要是負(fù)責(zé)收集表單數(shù)據(jù)的,在這里一定要注意表單的屬性必須和actionform中的get和set方法的屬性一致。這里依舊不深入解釋,隨后博客都會(huì)涉及到。       

?
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,負(fù)責(zé)表單收集數(shù)據(jù)
 * 表單的屬性必須和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類的建立,這個(gè)是負(fù)責(zé)取得表單數(shù)據(jù)、調(diào)用業(yè)務(wù)邏輯以及返回轉(zhuǎn)向信息。        

?
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
 * 負(fù)責(zé)取得表單數(shù)據(jù)、調(diào)用業(yè)務(wù)邏輯、返回轉(zhuǎn)向信息
 *
 * @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", "密碼錯(cuò)誤");
  }
  return mapping.findforward("error");
  }
}

      8、既然有轉(zhuǎn)向,那么我們還要建立兩個(gè)頁(yè)面,一個(gè)是登錄成功頁(yè)面,一個(gè)登錄失敗頁(yè)面。

           ①登錄成功頁(yè)面           

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

           ②登錄失敗頁(yè)面            

?
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、最后要進(jìn)行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>

       經(jīng)過(guò)配置之后實(shí)例就已經(jīng)做完了,感興趣童鞋可以自己手動(dòng)運(yùn)行一下。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费观看无人区完整版 | 2021麻豆剧果冻传媒入口永久 | 成人a级特黄毛片 | 午夜影视在线观看 | 欧美老少 | 5g影院天天影院天天爽影院网站 | 成人私人影院在线观看网址 | 免费在线观看伦理片 | 动漫人物差差差动漫人物免费观看 | 久久国产精品人妻中文 | 5g影院成人| www久久精品| 女仆色永久免费网站 | 久久内在线视频精品mp4 | 乳女教师欲乱动漫无修版动画3d | 亚洲欧美精品一区天堂久久 | 亚洲天堂视频在线观看免费 | 韩国女主播在线大尺无遮挡 | 精品99视频| 久久伊人精品青青草原2021 | 免费看国产一级片 | 我的青梅竹马是消防员2季未增删免费 | 色综合97天天综合网 | 性xxxx中国老妇506070 | 国产成人精品高清不卡在线 | 日本高清免费不卡在线播放 | 91精品免费国产高清在线 | 91看片淫黄大片在看 | 四虎永久在线精品国产馆v视影院 | 亚洲视频在线观看不卡 | 午夜一区二区三区 | 免费刷10000名片赞网站 | 高清毛片一区二区三区 | 欧美a一级片| 桃乃木香奈作品在线观看 | 国产成人精品高清在线观看99 | 亚洲高清无在码在线电影 | 传说之下羊妈挤羊奶网站 | 顶级欧美做受xxx000 | 欧美贵妇videos办公室360 | 久久综合中文字幕佐佐木希 |