用actionform截獲登錄表單數據
原登錄模塊用bookstore.user描述login.jsp登錄頁面表單的數據,在switch.jsp程序中通過<jsp:usebean>標簽獲取login.jsp表單的數據。其實user類相當于struts框架中的模型,我們將通過一個actionform更好地實現這個功能。
actionform和bean一樣以屬性名匹配的映射機制從http請求中填充對象數據,但actionform比一般的bean提供了更多的功能,struts允許actionform通過validate()方法進行自校驗,當數據不合法時自動轉向到輸出界面,此外還可以通過reset()方法,在數據填充前復位屬性值。
下面我們就來創建useractionform,替換user的功能,建立起struts框架中的"數據模型"。
1.指定actionform的web模塊及類信息
file->new...->web->在web頁中雙擊actionform圖標,彈出如圖 6所示的對話框:


按next到下一步。
3.一些附加功能的定義

直接按finish創建useractionform,再將user類的代碼拷貝過來,整改后的最終代碼如下所示:
代碼清單 6 以actionform實現的user類
1. package bookstore;
2.
3. import java.sql.*;
4. import java.text.*;
5. import java.util.date;
6. import javax.servlet.http.*;
7. import org.apache.struts.action.*;
8.
9. public class useractionform
10. extends actionform
11. {
12. private string userid;
13. private string password;
14. private string username;
15. private string logindatetime;
16. public string getpassword() {
17. return password;
18. }
19.
20. …
21. //復位所有屬性值
22. public void reset(actionmapping actionmapping,httpservletrequest servletrequest) {
23. this.userid = null;
24. this.username = null;
25. this.password = null;
26. this.logindatetime = null;
27. }
用action代替switch.jsp的控制轉換功能
我們在前面已經數落用switch.jsp實現請求轉換控制的缺點,struts框架的action是實現請求轉換控制的最適合替代者。
在這節里,我們就來創建一個名為loginaction的action,讓其完美的接替switch.jsp的工作。
file->new...->web->在web頁中雙擊action圖標,啟動創建action的向導。
1.指定action名字及web模塊



1. <%@page contenttype="text/html; charset=gbk" import="bookstore.userlist" %>
2. …
3. <form name="form1" method="post" action="/webmodule/loginaction.do">用戶名:
4. <select name="userid">
5. <option value="" selected>--登錄用戶--</option>
6. <%=userlist.getuserlisthtml()%>
7. </select>
8. 密 碼:
9. <input name="password" type="password">
10. <input type="submit" name="submit" value="登錄">
11. </form>
12. </body>
13. </html>
注意:
struts框架總控制器servlet通過路徑匹配的方式截獲http請求,其匹配串是*.do,表示url以.do結束的http請求才會被struts框架處理,否則struts忽略之。所以在寫鏈接地址時千萬不要忘了調用地址后加一個.do的后綴。

4.為/loginaction定義兩個出口
一個action一般只有一個入口,但往往會有多個出口,action根據業務處理的不同結果轉向相應的出口。圖 12 /loginaction右邊是一個帶"forward"的淺色虛框,右鍵單擊這個forward虛框,在彈出的菜單中點擊add forward菜單項,在strut config editor中將新增一個默認名為forward的出口項圖標,左鍵單擊這個forward新增的圖標,對這個出口進行制定,如圖 13所示:



1. <struts-config>
2. <form-beans>
3. <form-bean name="useractionform" type="bookstore.useractionform" />
4. </form-beans>
5. <action-mappings>
6. <action name="useractionform" path="/loginaction"
7. scope="request" type="bookstore.loginaction">
8. <forward name="success" path="/welcome.jsp" />
9. <forward name="fail" path="/fail.jsp" />
10. <forward name="error" path="/error.jsp" />
11. </action>
12. </action-mappings>
13. </struts-config>
1. package bookstore;
2.
3. import org.apache.struts.action.actionmapping;
4. import org.apache.struts.action.actionform;
5. import javax.servlet.http.httpservletrequest;
6. import javax.servlet.http.httpservletresponse;
7. import org.apache.struts.action.actionforward;
8. import org.apache.struts.action.action;
9.
10. import java.sql.*;
11.
12. public class loginaction
13. extends action {
14. public actionforward execute(actionmapping actionmapping,
15. actionform actionform,
16. httpservletrequest servletrequest,
17. httpservletresponse servletresponse) {
18. useractionform useractionform = (useractionform) actionform;
19. connection conn = null;
20. try
21. {
22. conn = dbconnection.getconnection();
23. preparedstatement pstat = conn.preparestatement(
24. "select user_name from t_user where user_id=? and password = ?");
25. pstat.setstring(1, useractionform.getuserid());
26. pstat.setstring(2, useractionform.getpassword());
27. resultset rs = pstat.executequery();
28. if (rs.next())
29. { //密碼正確
30. useractionform.setusername(rs.getstring(1));
31. servletrequest.getsession().setattribute("ses_userbean", useractionform);
32. return actionmapping.findforward("success");//通過驗證,轉向welcome.jsp出口
33. }
34. }
35. catch (sqlexception se)
36. {
37. se.printstacktrace();
38. return actionmapping.findforward("error");//程序發生異常,轉向error.jsp出口
39. }
40. finally
41. {
42. try
43. {
44. if (conn != null)
45. {
46. conn.close();
47. }
48. }
49. catch (sqlexception ex)
50. {
51. ex.printstacktrace();
52. return actionmapping.findforward("error");//程序發生異常,轉向error.jsp出口
53. }
54. }
55. return actionmapping.findforward("fail");//未通過驗證,轉向fail.jsp出口
56. }
57. }
<jsp:usebean id="ses_userbean" scope="session" class="bookstore.user"/>
<jsp:usebean id="ses_userbean" scope="session" class="bookstore.useractionform"/>
提示:
一般情況下,action只執行流程控制的功能,而不執行具體的業務處理。所以loginaction的execute()中驗證用戶的業務最好抽取到一個具體的bo中(business object:商業處理對象)。
新聞熱點
疑難解答