1、實例開始工作—導入jar包,在官網上下載struts1框架包,解壓之后導入工程的:
2、之后配置web.xml(這里的具體配置方法可以參見struts1框架包中的實例文件夾webapps中的實例代碼中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> |
首先這個配置文件中最主要的就是做了兩件的事情,一個是配置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>
經過配置之后實例就已經做完了,感興趣童鞋可以自己手動運行一下。