posts - 5, comments - 24, trackbacks - 0, articles - 20
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Struts學習筆記

          Posted on 2006-09-25 12:26 kook 閱讀(351) 評論(0)  編輯  收藏 所屬分類: Struts

          ?

          一、 如何獲得 ApplicationResources.properties 中的信息


          1
          、獲得默認的資源文件

          1?MessageResources?messages?= ?getResources(request);
          2?

          3?String?title?=?messages.getMessage("hello.jsp.title" );
          4?



          2
          、獲得默認的資源文件

          1?MessageResources?resources?= ?(MessageResources)?request.
          2?

          3 getAttribute(Globals.MESSAGES_KEY);
          4?



          3
          、獲得指定key的資源文件

          MessageResources?messages? = ?getResources(request, " en " );

          ?

          其實方法一本質上就是調用方法二來獲得 MessageResources 的實例。

          首先獲得一個封裝了 ApplicationResources.properties 信息

          的對象 MessageResources 的實例messages,然后調用getMessage(key);來獲得key的值

          二、 如何打包發布一個WEB應用

          DOC下轉到應用的根目錄執行jar cvf appName.war *.*

          三、 如何在formbean下驗證表單

          ?

          ?1?ActionErrors?errors?=?new ?ActionErrors();
          ?2?

          ?3?????String?array[]?=?new?String[2 ];
          ?4?

          ?5?????array[0]?=?"xx" ;
          ?6?

          ?7?????array[1]?=?"hh" ;
          ?8?

          ?9?????if(name==null?||?name.length()<1 )
          10?

          11? ????{
          12?

          13????????errors.add("name",new?ActionMessage("kong",false ));
          14?

          15?????//對應的key為hello,且hello為動態復合消息,array數組用來替換復合消息中的參數
          16?
          17????????errors.add("name",new?ActionMessage("hello" ,array));
          18?

          19? ????}
          20?

          21?return ?errors;
          22?

          23?

          ?

          inputJSP中用 <html:errors/> 獲取并顯示

          ?

          四、 action下處理業務邏輯時返回的錯誤信息

          ?1?ActionErrors?errors?=? new?ActionErrors();
          ?2?

          ?3
          ?String?userName? =?(String)((HelloForm)?form).getUserName();
          ?4?

          ?5
          ? ?
          ?6?

          ?7
          ?String?badUserName?=?"Monster ";
          ?8?

          ?9
          ? ?
          10?

          11
          ? if?(userName.equalsIgnoreCase(badUserName))?{
          12?

          13
          ??????errors.add("username",?new?ActionError("hello.dont.talk.to.monster ",?badUserName?));
          14?

          15
          ??????saveErrors(request,?errors);???????// 綁定到request對象中
          16
          ?

          17
          ??????return?( new?ActionForward(mapping.getInput()));
          18?

          19
          ? ?}
          20?


          同樣在相應的JSP頁面中用用<html:errors/>
          獲取并顯示

          ?

          五、 JSP 中如何訪問action

          1

          < A?href = " <%=request.getContextPath()?%>/hello.do " > Link </ A >

          ?

          2 、自動跳轉


          ???
          A_JSP
          頁面:

          < logic:forward?name = " gaga " />


          B_struts-config.xml
          設置:


          < global - forwards? >

          ??????<
          forward?name = " gaga " ?path = " /hello.do " ></ forward
          >

          </
          global - forwards >


          六、
          配置錯誤頁面和異常頁面:《精通Struts page_75


          七、
          如何在Web應用中訪問Locale對象


          1
          、什么是Locale?
          java.util.Locale 類的實例代表一種特定的語言環境和地區


          2
          如何獲得Locale

          2??????? 構造函數

          Locale?locale? = ? new ?Locale( " en " , " US ");
          Locale?locale?
          = ? new ?Locale( " ch " , " CH " );


          2????
          訪問java.util.Locale類的靜態常量來獲得常用的 Locale 實例

          Locale?locale? = ?Locale.CHINA;?????(zh_CN)
          Locale?locale?
          = ?Locale.CHINESE;???(zh)

          2??????? 通過HttpServletRequest對象來獲得客戶端的Locale

          Locale?locale? = ?request.getLocale();???// 獲得優先使用的Locale
          System.out.println(locale);

          ?

          // 獲得所有Locale,從優先級高的依次降序排列并打印出來。
          Enumeration?locales? = ?request.getLocales();

          while
          ?(locales.hasMoreElements())?{
          ??????Locale?element?
          =
          ?(Locale)?locales.nextElement();
          ??????System.out.println(element);
          }


          2???????
          通過HttpSession對象來獲得Locale

          HttpSession?session? = ?request.getSession();

          Locale?locale?
          =
          ?(Locale)?session.

          getAttribute(Globals.LOCALE_KEY);

          System.out.println(locale);

          ?

          獲得Locale的方法還有很多,其實在Web應用中,很多獲得Locale的方法都是不同程度的或者是直接的或者是間接在調用 request.getLocale(); 方法。因此有了 HttpServletRequest ,我們可以做很多事情。

          主站蜘蛛池模板: 鸡西市| 明光市| 青岛市| 肥乡县| 东乡县| 体育| 甘肃省| 永州市| 曲阜市| 南京市| 中宁县| 西城区| 南平市| 丘北县| 安岳县| 临夏市| 阿瓦提县| 行唐县| 襄汾县| 阿尔山市| 平陆县| 西乡县| 乐山市| 军事| 通许县| 驻马店市| 苏尼特左旗| 南昌县| 白水县| 宣汉县| 柳林县| 浮梁县| 刚察县| 阳谷县| 兰西县| 吴江市| 西乌珠穆沁旗| 凉山| 长兴县| 南安市| 常山县|