<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無為

    無為則可為,無為則至深!

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

    package tutorial;

    import java.util.List;

    import com.opensymphony.xwork2.ActionSupport;

    public class ProductConfirm extends ActionSupport {
    ? ?
    public List < Product > products;

    ? ?
    public List < Product > getProducts() {
    ? ? ? ?
    return products;
    ? ?}


    ? ?
    public void setProducts(List < Product > products) {
    ? ? ? ?
    this .products = products;
    ? ?}

    ? ?
    ? ?@Override
    ? ?
    public String execute() {
    ? ? ? ?
    for (Product p : products) {
    ? ? ? ? ? ?System.out.println(p.getName()
    + " | " + p.getPrice() + " | " + p.getDateOfProduction());
    ? ? ? ?}

    ? ? ? ?
    return SUCCESS;
    ? ?}

    }

    接看,在同上的包中加入ProductConfirm-conversion.properties,代碼如下:

    Element_products = tutorial.Product

    再在struts.xml文件中配置ProductConfirm Action,代碼片段如下:

    < action name ="ProductConfirm" class ="tutorial.ProductConfirm" >
    ? ?
    < result > /ShowProducts.jsp </ result >
    </ action >

    在WEB文件夾下新建AddProducts.jsp,內容如下:

    <% @ page ?contentType = " text/html; charset=UTF-8 " %>
    <% @taglib prefix = " s " uri = " /struts-tags " %>
    < html >
    < head >
    ? ?
    < title > Hello World </ title >
    </ head >
    < body >
    ? ?
    < s:form action ="ProductConfirm" theme ="simple" > ? ? ? ? ? ?
    ? ? ? ?
    < table >
    ? ? ? ? ? ?
    < tr style ="background-color:powderblue; font-weight:bold;" >
    ? ? ? ? ? ? ? ?
    < td > Product Name </ td >
    ? ? ? ? ? ? ? ?
    < td > Price </ td >
    ? ? ? ? ? ? ? ?
    < td > Date of production </ td >
    ? ? ? ? ? ?
    </ tr >
    ? ? ? ? ? ?
    < s:iterator value ="new int[3]" status ="stat" >
    ? ? ? ? ? ? ? ?
    < tr >
    ? ? ? ? ? ? ? ? ? ?
    < td >< s:textfield name ="%{'products['+#stat.index+'].name'}" /></ td >
    ? ? ? ? ? ? ? ? ? ?
    < td >< s:textfield name ="%{'products['+#stat.index+'].price'}" /></ td >
    ? ? ? ? ? ? ? ? ? ?
    < td >< s:textfield name ="%{'products['+#stat.index+'].dateOfProduction'}" /></ td >
    ? ? ? ? ? ? ? ?
    </ tr >
    ? ? ? ? ? ?
    </ s:iterator >
    ? ? ? ? ? ?
    < tr >
    ? ? ? ? ? ? ? ?
    < td colspan ="3" >< s:submit /></ td >
    ? ? ? ? ? ?
    </ tr >
    ? ? ? ?
    </ table >
    ? ?
    </ s:form > ? ?
    </ body >
    </ html >

    在同樣的文件夾下創建ShowProducts.jsp,內容如下:

    <% @ page ?contentType = " text/html; charset=UTF-8 " %>
    <% @taglib prefix = " s " uri = " /struts-tags " %>
    < html >
    < head >
    ? ?
    < title > Hello World </ title >
    </ head >
    < body > ? ?
    ? ?
    < table >
    ? ? ? ?
    < tr style ="background-color:powderblue; font-weight:bold;" >
    ? ? ? ? ? ?
    < td > Product Name </ td >
    ? ? ? ? ? ?
    < td > Price </ td >
    ? ? ? ? ? ?
    < td > Date of production </ td >
    ? ? ? ?
    </ tr >
    ? ? ? ?
    < s:iterator value ="products" status ="stat" >
    ? ? ? ? ? ?
    < tr >
    ? ? ? ? ? ? ? ?
    < td >< s:property value ="name" /></ td >
    ? ? ? ? ? ? ? ?
    < td > $ < s:property value ="price" /></ td >
    ? ? ? ? ? ? ? ?
    < td >< s:property value ="dateOfProduction" /></ td >
    ? ? ? ? ? ?
    </ tr >
    ? ? ? ?
    </ s:iterator >
    ? ?
    </ table >
    </ body >
    </ html >

    發布運行應用程序,在瀏覽器中鍵入http://localhost:8080/Struts2_Converter/AddProducts.jsp,出現如圖4所示頁面:
    圖4 添加產品頁面
    圖4 添加產品頁面

    按圖4所示,填寫表單,按“Submit”提交,出現圖5所示頁面:
    圖5 查看產品頁面
    圖5 查看產品頁面

    查看服務器的控制臺,有如下輸出:

    Expert One-on-One J2EE Development without EJB | 39.99 | Mon Jun 21 00 : 00 : 00 CST 2004
    Pro Spring |
    32.99 | Mon Jan 31 00 : 00 : 00 CST 2005
    Core J2EE Patterns: Best Practices and Design Strategies
    , Second Edition | 34.64 | Sat May 10 00 : 00 : 00 CST 2003

    上面的代碼并不復雜,但有幾點需要說明:

    1. ProductConfirm文件中的for(Product p : productes)的寫法是J2SE 5.0中的新特性,作用遍歷products列表;
    2. List<Product>也是J2SE 5.0的才有的泛型(Generic);
    3. ProductConfirm-conversion.properties中“Element_products=tutorial.Product”是告訴Struts 2.0列表products的元素的類型為Product,而不是定義轉換器;
    4. 在AddProducts.jsp的<s:textfield>的name為“%{'products['+#stat.index+'].name'}”,%{exp}格式表示使用OGNL表達式,上述表達式的相當于<%= "products[" + stat.index + "].name" %>,至于<s:iterator>標志的用法可以參考我之前的文章《常用的Struts 2.0的標志(Tag)介紹》。

    轉換錯誤處理

    不知道大家在運行上面的例子時,有沒有填錯日期或數字情況,又或者您有沒有思考過這種情況?如果還沒有嘗試的朋友可以試一下,在第一行的Price和Date of production中輸入英文字母,然后按“Submit”提交。你會看到頁面為空白,再看一下服務器的控制臺輸出,有如下語句: 警告: No result defined for action tutorial.ProductConfirm and result input,它提示我們沒有為Action定義輸入結果,所以,我們應該在源代碼文件夾下的struts.xml中的ProductConfirm Action中加入以下代碼:

    < result name ="input" > /AddProducts.jsp </ result >

    重新加載應用程序,刷新瀏覽器重新提交請求,這時頁面返回AddProducts.jsp,格式錯誤的輸入框的值被保留,如下圖6所示:
    圖6 沒有提示的錯返回頁面
    圖6 沒有提示的錯返回頁面

    當然,我們還可以在頁面上加上錯誤提示信息,通過在AddProducts.jsp的“<body>”后,加入下面代碼可以實現:

    < div style ="color:red" >
    ? ?
    < s:fielderror />
    </ div >

    刷新瀏覽器,重新提交請求,出現如圖7所示頁面:
    圖7 帶提示的錯返回頁面
    圖7 帶提示的錯返回頁面

    以上的功能的都是通過Struts 2.0里的一個名為conversionError的攔截器(interceptor)工作,它被注冊到默認攔截器棧(default interceptor stack)中。Struts 2.0在轉換出錯后,會將錯誤放到ActionContext中,在conversionError的作用是將這些錯誤封裝為對應的項錯誤(field error),因此我們可以通過<s:fielderror />來將其在頁面上顯示出來。另外,大家看第二和第三行的Price都被賦為0.0的值,而第一行則保留其錯誤值。這同樣是conversionError的功勞——沒有出錯的行調用的products[index].price(默認值為0.0),而出錯的行則會被賦為頁面所提交的錯誤值,這樣可以提供更好的用戶體驗。

    總結

    Struts 2.0的轉換器簡化的WEB應用程序的模型,為我們的編程帶來極大的方便。

    posted on 2006-11-07 14:26 Max 閱讀(571) 評論(2) ?編輯?收藏引用收藏至365Key 所屬分類: Struts 2.0系列


    凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
    、轉載請注明來處和原文作者。非常感謝。

    posted on 2006-11-08 22:57 草兒 閱讀(297) 評論(0)  編輯  收藏 所屬分類: java
    主站蜘蛛池模板: 欧美在线看片A免费观看| 无码一区二区三区免费视频| 亚洲av片一区二区三区| 精品国产日韩亚洲一区91| 暖暖在线日本免费中文| 亚洲成AV人片在WWW| 国产免费怕怕免费视频观看| 色吊丝免费观看网站| 精品亚洲成α人无码成α在线观看 | 亚洲AV无码精品色午夜果冻不卡 | 内射干少妇亚洲69XXX| 久久精品一本到99热免费| 午夜亚洲AV日韩AV无码大全| 精品熟女少妇av免费久久| 久久狠狠爱亚洲综合影院| 免费看AV毛片一区二区三区| 亚洲AV无码国产一区二区三区| 国产无遮挡色视频免费视频| 免费人成视频在线观看免费| 亚洲乱码中文字幕手机在线| 人妻免费一区二区三区最新| 亚洲欧洲日本国产| 国产婷婷高清在线观看免费| 又粗又长又爽又长黄免费视频| 亚洲精品二区国产综合野狼| 国产成人精品久久免费动漫| 色婷婷六月亚洲综合香蕉| 国产亚洲老熟女视频| 亚洲综合免费视频| 亚洲国产成人久久精品大牛影视| 亚洲午夜精品一级在线播放放 | 久久香蕉国产线看观看亚洲片| 18禁黄网站禁片免费观看不卡 | 四虎一区二区成人免费影院网址| 亚洲乱码中文字幕综合| 99re免费99re在线视频手机版| 亚洲性色精品一区二区在线| 亚洲综合网站色欲色欲| 国产免费AV片在线播放唯爱网| 一级毛片免费播放男男| 亚洲欧洲尹人香蕉综合|