環(huán)境:myeclipse 14
1 struts1 框架搭建
在myeclipse新建web project 取名為struts1_login,此時(shí)是一個(gè)空文檔就不截圖了然后在project上右鍵->選擇myeclipse->add struts capabilities
單擊上面install apache struts(1.x)facet
點(diǎn)擊next
選擇*.do ,改下包名改成與你項(xiàng)目相關(guān)的。如我的包名為com.lichang.struts1
點(diǎn)擊next
點(diǎn)擊完成,在我們的web-inf下就會(huì)多出struts-config.xml文件
以上就是讓myeclipse幫我們加入框架的大概過(guò)程。項(xiàng)目的整體結(jié)構(gòu)如下:
至此我們的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> </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_login.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。