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

          Struts學習筆記

          Posted on 2006-09-25 12:26 kook 閱讀(352) 評論(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 ,我們可以做很多事情。

          主站蜘蛛池模板: 宜昌市| 东乌| 油尖旺区| 杨浦区| 化隆| 克拉玛依市| 陆良县| 张家口市| 将乐县| 长乐市| 鹰潭市| 衢州市| 安徽省| 昌平区| 盈江县| 丹巴县| 蓬莱市| 专栏| 百色市| 繁峙县| 勐海县| 义乌市| 马尔康县| 依安县| 重庆市| 怀远县| 镇雄县| 承德市| 六安市| 通江县| 永宁县| 太湖县| 永川市| 梁平县| 丹巴县| 自治县| 团风县| 寻乌县| 长春市| 贡山| 微博|