<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
</struts-config>



string bookid;//圖書id,對應t_book表的book_id,是主鍵。
string isbn;//isbn
string createdate;//創建日期
string bookname;//書名
string author;//作者
1. package bookstore;
2.
3. import javax.servlet.http.httpservletrequest;
4. import org.apache.struts.action.*;
5. import java.sql.*;
6.
7. public class bookactionform
8. extends actionform {
9. …
10. public actionerrors validate(actionmapping actionmapping,
11. httpservletrequest httpservletrequest) {
12. actionerrors errors = new actionerrors();
13. connection conn = null;
14. try {
15. conn = dbconnection.getconnection();
16. preparedstatement pstat = conn.preparestatement(
17. "select count(*) count from t_book where book_id=?");
18. pstat.setstring(1, this.bookid);
19. resultset rs = pstat.executequery();
20. if (rs.next()&& rs.getint(1) > 0) {
21. errors.add("bookid ",
22. new actionmessage("bookstore.duplicate.bookid",
23. "圖書id和數據庫中已經有的id重復"));
24. }
25. }
26. catch (sqlexception se) {
27. se.printstacktrace();
28. errors.add("bookid",
29. new actionmessage("bookstore.dbaccess.error", "訪問數據庫時出錯"));
30. }
31. finally {
32. try {
33. if (conn != null) {
34. conn.close();
35. }
36. }
37. catch (sqlexception ex) {
38. ex.printstacktrace();
39. errors.add("bookid",
40. new actionmessage("bookstore.dbaccess.error",
41. "訪問數據庫時出錯"));
42. }
43. }
44. return errors;
45. }
46.
47. public void reset(actionmapping actionmapping,
48. httpservletrequest servletrequest) {
49. this.createdate = getcurrdatestr();
50. }
51.
52. //獲取當前時間字符
53. private static string getcurrdatestr() {
54. simpledateformat sdf = new simpledateformat("yyyymmdd");
55. return sdf.format(new date());
56. }
57. }
當用戶提交表單后,struts框架自動把表單數據填充到actionform中,接著struts框架自動調用actionform的validate()方法進行數據驗證。如果validate()方法返回的actionerrors為null或不包含任何actionmessage對象,表示通過驗證,struts框架將actionform和http請求一起傳給action的execute(),否則struts框架將http請求返回到輸入的頁面中,而輸入頁面即可通過<html:errors>顯示對應request域中的actionerrors錯誤信息顯示出來。
此外,我們在reset()方法中將createdate屬性置為當前的日期,因為這個屬性值不是通過頁面表單提供的。
新增圖書jsp頁面
1.通過向導創建bookadd.jsp
通過jsp向導創建bookadd.jsp頁面,在向導的第2步選擇使用struts1.1的struts-bean和struts-html標簽,如圖 19所示:


1. <%@page contenttype="text/html; charset=gbk" %>
2. <%@taglib uri="/web-inf/struts-bean.tld" prefix="bean"%>
3. <%@taglib uri="/web-inf/struts-html.tld" prefix="html"%>
4. <html>
5. <head>
6. <title>bookinsert</title>
7. <script language="javascript" >
8. function mysubmit(form)
9. {
10. if(form.isbn.value == null || form.isbn.value == "")
11. {
12. alert("圖書的isbn不允許為空");
13. return false;
14. }
15. if(form.bookname.value == null || form.bookname.value == "")
16. {
17. alert("圖書名不允許為空");
18. return false;
19. }
20. }
21. </script>
22. </head>
23. <body bgcolor="#ffffff">
24. <html:errors/>
25. <html:form action="/bookinsertaction.do" focus="bookid" method="post"
26. onsubmit="return mysubmit(this)" >
27. ?。紅able width="100%%" border="0">
28. ?。紅r>
29. <td>
30. <bean:message bundle="bookstore" key="bookstore.bookid"/>
31. </td>
32. <td>
33. <html:text name="bookactionform" property="bookid"/>
34. ?。?td>
35. <td>
36. ?。糱ean:message bundle="bookstore" key="bookstore.isbn"/>
37. ?。?td>
38. ?。紅d>
39. ?。糷tml:text name="bookactionform" property="isbn"/>
40. </td>
41. ?。?tr>
42. ?。紅r>
43. ?。紅d>
44. <bean:message bundle="bookstore" key="bookstore.bookname"/>
45. </td>
46. <td>
47. ?。糷tml:text name="bookactionform" property="bookname"/>
48. </td>
49. <td>
50. ?。糱ean:message bundle="bookstore" key="bookstore.author"/>
51. ?。?td>
52. ?。紅d>
53. ?。糷tml:text name="bookactionform" property="author"/>
54. ?。?td>
55. ?。?tr>
56. ?。紅r align="center">
57. ?。紅d colspan="4">
58. <html:submit value="保存"/>
59. ?。糷tml:reset value="取消"/>
60. </td>
61. ?。?tr>
62. ?。?table>
63. </html:form>
64. </body>
65. </html>

bookstore.bookid=/u56fe/u4e66idbookstore.isbn=isbnbookstore.bookname=_
/u56fe/u4e66/u540dbookstore.author=/u4f5c/u8005bookstore.dbaccess.error=_
/u6570/u636e/u5e93/u8bbf/u95ee/u9519/u8befbookstore.duplicate.bookid=_
/u56fe/u4e66/u7f16/u53f7/u548c/u6570/u636e/u5e93/u4e2d/u5df2/u6709/u7684/u7f16/u53f7/u91cd/u590d
bookstore.bookid=圖書idbookstore.isbn=isbnbookstore.bookname=圖書名bookstore.author=作者bookstore.dbaccess.error=數據庫訪問錯誤bookstore.duplicate.bookid=圖書編號和數據庫中已有的編號重復
提示:
jdk提供了一個將中文轉換為unicode編碼格式的工具native2ascii.exe,它位于<jdk>/bin/目錄下。在dos命令窗口下,通過native2ascii -encoding gbk <源文件>
?。寄繕宋募炯纯梢酝瓿赊D換。
注意:
在前文中,我們曾提到了jbuilder 2005的一個bug,即web模塊中的類或資源有時得不到同步,需要手工將類和資源拷貝到對應的目錄。如果你發現資源文件沒有同步到web-inf/classes目錄時,bookstoreresource_zh_cn.properties需要在編譯工程后手工拷貝到這個目錄下,否則struts就無法找到資源了。

創建bookinsertaction
下面,我們來創建bookinsertaction,在該action中將圖書記錄添加到t_book表中。如果操作成功定向到insertsuccess.htm操作成功頁面,如果在進行數據庫操作時發現sqlexception,則轉向sqlfail.htm頁面。我們需要事先創建這兩個html頁面,為了簡單,僅在其中寫入報告操作成功和失敗的信息即可。
按3.2相似的方式創建bookinsertaction,用book-struts-config.xml記錄配置信息,在向導的第2步,將formbean name指定為bookactionform,scope為request,將input jsp指定為/bookadd.jsp,如圖 23所示:


1. package bookstore;
2.
3. import javax.servlet.http.*;
4. import org.apache.struts.action.*;
5. import java.sql.*;
6.
7. public class bookinsertaction
8. extends action {
9. public actionforward execute(actionmapping actionmapping,
10. actionform actionform,
11. httpservletrequest servletrequest,
12. httpservletresponse servletresponse)
13. throws exception
14. {
15. bookactionform bookactionform = (bookactionform) actionform;
16. connection conn = null;
17. conn = dbconnection.getconnection();
18. preparedstatement pstat = conn.preparestatement(
19. " insert into t_book1(book_id,isbn,book_name,author,"+
20. "create_date) values(?,?,?,?,?)");
21. pstat.setstring(1, bookactionform.getbookid());
22. pstat.setstring(2, bookactionform.getisbn());
23. pstat.setstring(3, bookactionform.getbookname());
24. pstat.setstring(4, bookactionform.getauthor());
25. pstat.setstring(5, bookactionform.getcreatedate());
26. pstat.executeupdate();
27. return actionmapping.findforward("success");
28.
29. }
30. }


1. …
2. <struts-config>
3. …
4. ?。糶lobal-exceptions>
5. ?。糴xception key="sqlexception" type="java.sql.sqlexception"
6. path="/sqlfail.htm"/>
7. </global-exceptions>
8. …
9. </struts-config>

新聞熱點
疑難解答