1、實(shí)例開(kāi)始工作—導(dǎo)入jar包,在官網(wǎng)上下載struts1框架包,解壓之后導(dǎo)入工程的:
2、之后配置web.xml(這里的具體配置方法可以參見(jiàn)struts1框架包中的實(shí)例文件夾webapps中的實(shí)例代碼中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
|
<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)行一下。