国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

JBuilder2005 Struts深度體驗之新增

2024-07-21 02:15:17
字體:
來源:轉載
供稿:網友
  新增一個struts配置文件

  考慮到圖書模塊是一個比較獨立的模塊,為了避免對struts配置文件的資源爭用導致團隊工程的覆蓋或沖突,我們為這個模塊單獨提供一個新的struts配置文件,用這個配置文件配置圖書模塊所有struts關聯的信息。

  我們按照如下的方式為webmodule模塊添加一個名為book-struts-config.xml的配置文件。

  首先到<工程目錄>/webmodule/web-inf拷貝一個原有的struts-config.xml文件,更名為book-struts-config.xml放在struts-config.xml相同的目錄下,刪除原有配置的內容,將其調整成:

<?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>


  然后,在工程窗格的資源樹中定位到webmodule->deployment descriptors-><struts 1.1>節點上,右擊<struts 1.1>節點,在彈出的菜單中選擇properties...彈出properties for ’<struts 1.1>’對話框,如圖 16所示:


圖 16 struts配置文件維護對話框

  點擊add...按鈕,在彈出的choose struts config file對話框中選擇book-struts-config.xml配置文件,按ok這個新的struts配置文件將添加到struts config file in web.xml列表中。

  新增配置文件成功后,在工程窗格資源樹的<struts 1.1>節點下,你將會發現這個新加入的struts配置文件,如下圖所示:


圖 17 兩個struts配置文件

  這樣,在創建新的formbean或action時,你就可以選擇用哪個配置文件來保存struts的配置信息了。

  圖書action form

  下面我們著手創建用于接收新增圖書頁面表單數據的bookactionform,使用book-struts-config.xml保存bookactionform的配置信息。bookactionform需要進行數據有效性自檢,也就是說,要讓bookactionform實現validate()方法。

  創建bookactionform和創建useractionform相似,但是在向導的第1步需要指定book-struts-config.xml記錄bookactionform配置信息,如圖 18所示:


圖 18 選擇不同的配置文件

  我們在前一節為web模塊添加了一個配置文件,在struts config下拉框中列出了web模塊所有配置文件,這里我們選擇web-inf/book-struts-config.xml。

  在向導的第2步,我們為bookactionform定義下列5個屬性:

string bookid;//圖書id,對應t_book表的book_id,是主鍵。
string isbn;//isbn
string createdate;//創建日期
string bookname;//書名
string author;//作者


  在向導的第2步直接按finish創建bookactionform。由于bookid屬性是主鍵,所以不能和t_book中已有的記錄重復,這可以通過bookactionform的數據自檢機制來完成,數據自檢是通過定義validate()方法來實現的。向導已經為bookactionform生成了validate()方法框架,我們只需要在validate()方法編寫bookid的校驗的代碼就可以了,bookactionform的最終代碼如代碼清單 10所示:

  代碼清單 10 bookactionform.java

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所示:


圖 19 指定選用struts標簽

  2.使用jbuilder的struts標簽構建jsp頁面

  你可以直接用拖拽的方法從jbuilder編輯器左邊的標簽庫將struts標簽添加到jsp頁面中,如圖 20所示:


圖 20 用拖拽的方式添加struts標簽

  struts的html標簽可以完成和標準的html元素相同的功能,struts提倡使用struts html標簽庫,因為這些標簽可以和struts框架的其他組件緊密地聯系起來。而strtus的bean標簽庫可以訪問已經存在的javabean及其屬性,有一些bean標簽還可以訪問http請求頭信息及web資源文件的信息。

  我們希望用struts的html標簽庫創建添加圖書的表單,通過bean標簽庫訪問web資源文件作為表單組件前的標識文字。

  bookadd.jsp的最終代碼如代碼清單 11所示:

  代碼清單 11 bookadd.jsp

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>


  其中第25~63行是表單的定義代碼,將<html:form>的action指定為"/bookinsertaction.do", 它是bookinsertaction的訪問uri,將在下一節實現,通過<html:form>訪問action時,action只需保證和配置文件中指定的path一致就可以了,無需在前面添加上諸如/webmodule的web部署子目錄。

  在第26行我們為<html:form>指定了一個onsubmit客戶端校驗函數,當isbn和bookname兩組件中的任何一個為空時,拒絕提供表單。

  我們定義了4個<html:text>,它們對應標簽html的<input type="text">輸入框標簽,其中name屬性為對應的actionform名字,而property對應actionform的屬性。圖 21是bookadd.jsp的設計期效果圖:


圖 21 bookadd.jsp設計時的界面圖

  當然,你可以直接在表單組件前寫入具體的標識,如"圖書id",而非第30行的<bean:message>標簽,但后者通過一個資源文件產生具體的標識,這樣不但可直接通過資源文件控制標識還提供了國際化的特性。

  上面,我們只是簡單地引用了名為bookstore的資源文件,下面我們需要創建這個資源文件并在struts配置文件中描述它。

  到<工程目錄>/src目錄下,創建一個名為bookstoreresource_zh_cn.properties的資源文件,其內容如下所示:

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


  這兒的中文必須采用unicode編碼格式,其對應的中文文件的內容為:

bookstore.bookid=圖書idbookstore.isbn=isbnbookstore.bookname=圖書名bookstore.author=作者bookstore.dbaccess.error=數據庫訪問錯誤bookstore.duplicate.bookid=圖書編號和數據庫中已有的編號重復


  在編譯工程時,jbuilder會將位于<工程目錄>/src下的資源文件拷貝到web模塊的web-inf/classes目錄下。

  提示:

  jdk提供了一個將中文轉換為unicode編碼格式的工具native2ascii.exe,它位于<jdk>/bin/目錄下。在dos命令窗口下,通過native2ascii -encoding gbk <源文件>

 ?。寄繕宋募炯纯梢酝瓿赊D換。


  注意:

  在前文中,我們曾提到了jbuilder 2005的一個bug,即web模塊中的類或資源有時得不到同步,需要手工將類和資源拷貝到對應的目錄。如果你發現資源文件沒有同步到web-inf/classes目錄時,bookstoreresource_zh_cn.properties需要在編譯工程后手工拷貝到這個目錄下,否則struts就無法找到資源了。


  在工程窗格的資源樹中找到book-struts-config.xml雙擊打開其對應的struts config editor,切換到message resources標簽頁,如所示:


圖 22 定義資源文件

  通過add...按鈕定義一個鍵名為bookstore的bookstoreresource資源文件。不同的語言環境的客戶端將會訪問不同的資源文件,如客戶端為中文環境時,將訪問bookstoreresource_ch_cn.properties,如果是英語的客戶端將訪問bookstoreresource_cn.properties資源文件,如果沒有找到對應語言的資源文件,將訪問默認的資源文件,這里,默認資源文件為bookstoreresource.properties。<bean:message>的boudle即為parameter欄定義的名稱。

  實戰經驗:

  筆者原來對struts標簽是比較排斥的,因為雖說它本意希望將頁面展現邏輯和程序邏輯完美地分離開來。頁面設計的工作由頁面設計人員在dreamweaver中完成,由于dreamweaver不認識struts的標簽,那些像<html:text>,<html:submit>等表單組件的struts標簽在頁面設計時看不到效果,所見即所得的理念被無情的剝奪了,界面設計人員對包含struts標簽的頁面常常感到如墮五里霧中。

  許多struts開發人員一直夢寐以求,希望能得到一個dreamweaver的struts標簽擴展插件,讓頁面設計人員可以象標準的html組件標簽一樣設計頁面。fwa公司的visual tags for struts插件終于使我們夢想成真。通過這一插件,可以將dreamweaver完美地和struts結合起來,使用struts標簽的jsp頁面可以在設計期得到使用標準html標簽一樣的可視化效果。你可以通過http://www.fwasi.com/downloads/下載visual tags for struts的試用版。圖 21的bookadd.jsp效果界面,即是在dreamweaver 2004中使用了visual tags for struts插件后的效果頁面。

  此外,有一點關于struts標簽的事情也必須提及:sun開發出了jsf頁面標簽,這和struts的標簽在功能上是有重疊的,struts 以后的版本將逐漸往jsf靠近,jsf的標簽可能將最終取代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所示:


圖 23 指出bookinsertaction的配置信息

  按finish直接創建bookinsertaction,jbuilder將打開struts config editor顯示/bookinsertaction的流程,如圖 24所示:


圖 24 bookinsertaction流程

  添加1個出口,名為success,路徑為/insertsuccess.htm。最終的/bookinsertaction的流程如圖 5所示。

  代碼清單 12是bookinsertaction的代碼,它完成圖書添加,出口控制的操作:

  代碼清單 12 bookinsertaction.java

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. }


  bookinsertaction將bookactionform的數據通過jdbc添加到t_book表中,添加成功則轉向insertsuccess.htm頁面。有些觀察細致的讀者也許已經注意到bookinsertaction的execute()方法并未直接對sqlexception進行處理,而是將異常拋出,如第13行所示。這里,我們要用到struts1.1的新功能:通過配置方式處理異常。

  在工程窗格的webmodule/deployment descriptors/<struts 1.1>下找到并雙擊book-struts-config.xml文件,調出的struts config editor配置編輯器,切換到global exceptions標簽頁,如圖 25所示:


圖 25 異常處理配置

  點擊add...定義一個名為sqlexception的異常處理配置項,處理java.sql.sqlexception異常,定義完這個配置項后,選中這個配置項,點擊edit...切換到這個配置項的詳細設置頁面,如圖 26所示:


圖 26 異常處理配置窗口

  在窗口下部切換到source視圖頁中,這個異常配置項的配置信息如代碼清單 13所示:

  代碼清單 13 sqlexception的異常處理配置項

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>


  第5~6行將/sqlfail.htm和java.sql.sqlexception"掛接"起來,這樣程序中任何action的execute()方法拋出的sqlexception異常都將由sqlexception配置項處理。

  在welcome.jsp頁面中添加一個調用bookadd.jsp的鏈接,登錄系統后,通過這外鏈接調出bookadd.jsp頁面,如圖 27所示:


圖 27 添加圖書頁面

  填寫圖書記錄,點擊保存提交表單到/bookinsertaction的action中,如果提供的圖書id和t_book中已有的book_id重復,則struts總控制器將直接返回到bookadd.jsp頁面中,由<html:errors/>報告錯誤信息。如果數據通過bookactionform的自檢并成功添加到數據庫中,將最終轉向insertsuccess.htm頁面。

  提示:

  如果用tomcat為web服務器,如果圖書記錄會出現中文亂碼的問題,可以使用我們上面介紹過的一個編碼過濾器,你可以在web.xml中配置這個過濾器,亂碼問題就可以解決了。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 时尚| 买车| 尤溪县| 于都县| 玉树县| 和田市| 松阳县| 从江县| 铜川市| 岳普湖县| 东丰县| 深州市| 陵川县| 临海市| 长泰县| 吴川市| 丹棱县| 崇义县| 江华| 高唐县| 当涂县| 旬阳县| 汾西县| 通城县| 开化县| 金乡县| 定远县| 金溪县| 应城市| 湟中县| 綦江县| 宜兴市| 北辰区| 望奎县| 临海市| 广元市| 白玉县| 昌江| 巴彦县| 瓦房店市| 治县。|