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

          ActionMessages類及其綁定的組件

          Posted on 2006-09-25 02:54 kook 閱讀(1546) 評(píng)論(4)  編輯  收藏 所屬分類: Struts

          首先來了解一下ActionMessages類的結(jié)構(gòu):



          在ActionMessages中包含著一個(gè)HashMap,其中的key就是add方法的property參數(shù),然而他的值對(duì)應(yīng)的是一個(gè)ActionMessageItem,ActionMessageItem是該類的一個(gè)內(nèi)部類,當(dāng)中包含了一個(gè)ArrayList。
          由此可見,一個(gè)property中可以包含著多個(gè)ActionMessage,這些ActionMessage都將保存在ActionMessageItem的ArrayList中。

          ?1?public?void ?add(String?property,?ActionMessage?message)?{
          ?2?

          ?3?????????ActionMessageItem?item?= ?(ActionMessageItem)?messages.get(property);
          ?4?????????List?list?=?null
          ;
          ?5?

          ?6?????????if?(item?==?null )?{
          ?7?????????????list?=?new
          ?ArrayList();
          ?8?????????????item?=?new?ActionMessageItem(list,?iCount++
          ,?property);
          ?9?

          10? ????????????messages.put(property,?item);
          11?????????}?else
          ?{
          12?????????????list?=
          ?item.getList();
          13?
          ????????}
          14?

          15? ????????list.add(message);
          16?

          17?????}



          每次添加新的ActionMessage中,ActionMessages類會(huì)判斷HashMap中的get(property)是否存在,如果存在的話,就獲得該list?=?item.getList();并把ActionMessage添加到其中。如果不存在,就會(huì)創(chuàng)建一個(gè)新的list?=?new ?ArrayList();
          再將內(nèi)容添加到當(dāng)中去。總的來說,ActionMessages就是一個(gè)保存信息的容器。通常ActionMessages是不會(huì)單獨(dú)使用的,和他相關(guān)的一些Struts組件會(huì)對(duì)其進(jìn)行訪問和操作:

          <html:errors>標(biāo)簽

          <html:errors>標(biāo)簽是一個(gè)Struts組件,他的內(nèi)部標(biāo)簽類對(duì)ActionMessages類的子類ActionErrors進(jìn)行訪問。通過在request和session中找到ActionErrors來獲得相關(guān)信息。在此之前,先來了解下<html:errors>標(biāo)簽的幾個(gè)重要屬性:

          name屬性:
          指定ActionMessages在request和session范圍內(nèi)的屬性key.默認(rèn)值為Globals.ERROR_KEY。由此我們可以了解到Struts是如此保存和獲取ActionMessages的:

          保存:
          ActionErrors errors = new ActionErrors();

          request.setAttribute(Globals.ERROR_KEY ,errors);或者

          session.setAttribute(Globals.ERROR_KEY ,errors);

          獲取:
          ActionErrors errors = new ActionErrors();

          errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY);或者

          errors = (ActionErrors)session.getAttribute(Globals.ERROR_KEY);或者

          該屬性通常我們不用設(shè)置和更改他,用默認(rèn)值就可以啦。

          property屬性:
          用來指定消息的屬性,如果不設(shè)置該屬性,那么<html:errors>標(biāo)簽將顯示ActionMessages中的所有屬性。如果設(shè)置了該屬性,則只顯示HashMap中Key為property屬性值的ArrayList集合。通常,我們在調(diào)用ActionMessages的add方法時(shí),會(huì)指定add方法的property參數(shù),也就是第一個(gè)參數(shù),將相關(guān)信息添加到指定的property對(duì)應(yīng)的ArrayList中。那么我們要顯示相關(guān)信息時(shí),就可以指定輸出<html:errors>標(biāo)簽的property屬性對(duì)應(yīng)的ArrayList,這兩個(gè)property是相互對(duì)應(yīng)的。

          bundle屬性:
          指定資源文件的key屬性。缺省的情況下調(diào)用的是默認(rèn)的資源文件Application.properties。
          如在struts-config.xml中聲明的資源文件信息如下:

          ? < message - resources?parameter = " com.kook.struts.ApplicationResources " ? />
          ??
          < message - resources?key = " ch " ?parameter = " com.kook.struts.ApplicationResources_ch " ? />
          ??
          < message - resources?key = " en " ?parameter = " com.kook.struts.ApplicationResources_en " ? />

          對(duì)應(yīng)的add方法如下:

          1?ActionErrors?errors?=?new ?ActionErrors();
          2?if(name==null?||?name.length()<1
          )
          3?
          {
          4???????errors.add("name",new?ActionMessage("kong"
          ));
          5?
          ?}
          6??return?errors;

          new ?ActionMessage( " kong " )對(duì)應(yīng)在key="en"的資源文件中為:

          kong=bu neng wei kong

          JSP中的<html:errors>標(biāo)簽:

          1?<html:errors?property="name"?bundle="en"/>

          這時(shí)候?qū)?huì)輸出key="en"的資源文件中的"kong"對(duì)應(yīng)的信息:bu neng wei kong

          <html:messages>標(biāo)簽

          <html:messages>標(biāo)簽是用來在JSP頁面上輸出一條消息的。通常我們在做添加數(shù)據(jù)的時(shí)候,如果添加成功,通常會(huì)在前臺(tái)頁面反饋給用戶一條“添加成功”的信息,這時(shí)候我們就可以用到這個(gè)標(biāo)簽。

          name屬性:

          是指ActionMessages對(duì)象保存在request或session中的屬性key,即request.setAttribute("heihei", messages);中的第一個(gè)參數(shù)。

          message屬性:如果為true,表示是從request或session中取得key為Globals.MESSAGE_KEY的ActionMessages對(duì)象,此時(shí)該標(biāo)簽設(shè)置的name屬性的無效的。如果為false,表示從該標(biāo)簽的name屬性來獲得request或session的key值。缺省值就為false。

          id屬性:這個(gè)屬性就不說了,檢索出ActionMessages集合中單個(gè)對(duì)象的標(biāo)識(shí),他和<logic:iterate>的id是同一個(gè)意思。

          小實(shí)驗(yàn):

          在action中的execute方法中添加如下代碼:

          1?ActionMessages?messages?=?new?ActionMessages();
          2?????????
          3 messages.add("",?new?ActionMessage("haha",false));
          4?????????
          5?this.saveMessages(request,?messages);????????
          6?
          7?return?new?ActionForward("/form/hello.jsp");

          表示往ActionMessages對(duì)象中添加一條信息new?ActionMessage("haha",false),并保存在request當(dāng)中this.saveMessages(request,?messages);??那么在這個(gè)方法中,他是如何保存ActionMessage對(duì)象的呢?下面是?saveMessages?方法的源代碼:?

          ?1?protected?void?saveMessages(
          ?2?????????HttpServletRequest?request,
          ?3?????????ActionMessages?messages)?{
          ?4?
          ?5?????????//?Remove?any?messages?attribute?if?none?are?required
          ?6?????????if?((messages?==?null)?||?messages.isEmpty())?{
          ?7?????????????request.removeAttribute(Globals.MESSAGE_KEY);
          ?8?????????????return;
          ?9?????????}
          10?
          11?????????//?Save?the?messages?we?need
          12?????????request.setAttribute(Globals.MESSAGE_KEY,?messages);
          13?????}

          觀察這段代碼不難發(fā)現(xiàn),他是將ActionMessages存放在key為Globals.MESSAGE_KEY當(dāng)中的,因此我們在調(diào)用ActionMessages的add方法時(shí),他的property參數(shù)(add方法的第一個(gè)參數(shù))就沒有必要讓我們自己去手動(dòng)指定了,因?yàn)椴徽撃阒付ㄊ裁磪?shù),只要調(diào)用了saveMessages方法(這是前提),他只會(huì)將信息保存在固定的常量key中。
          這時(shí)前臺(tái)的hello.jsp頁面的顯示代碼為:

          1?<html:messages?id="a"?message="true">
          2?????<bean:write?name="a"/>
          3?</html:messages>

          上面代碼設(shè)置了message屬性,表示從Globals.MESSAGE_KEY中獲取信息,前面也有提到,此時(shí)指定name屬性是無效的。

          那如果我想只顯示我自己指定的信息怎么辦呢?

          我們可以不使用saveMessages方法而使用最原始的辦法,通常最原始的辦法也是最有效的,Struts其實(shí)是封裝了許多最原始的實(shí)現(xiàn)。我們先將action中的代碼改為:

          ActionMessages?messages? = ? new ?ActionMessages();
          ????????
          messages.add(
          "" ,? new ?ActionMessage( " haha " , false ));
          ????????
          request.setAttribute(
          " show " ,?messages);

          return ? new ?ActionForward( " /form/hello.jsp " );

          然后在JSP頁面中指定name屬性,而將message設(shè)為false就可以了:

          1?<html:messages?id="a"?name="show">
          2?????<bean:write?name="a"/>
          3?</html:messages>

          這里是直接省略了message屬性,因?yàn)樗娜笔≈稻褪莊alse。這樣就可以達(dá)到輸出自己指定屬性的信息啦!




          ?


          評(píng)論

          # re: ActionMessages類及其綁定的組件  回復(fù)  更多評(píng)論   

          2007-05-22 17:40 by cqusoft
          good!

          # re: ActionMessages類及其綁定的組件  回復(fù)  更多評(píng)論   

          2007-06-11 01:34 by samalanqi
          request.setAttribute( " show " , messages);為什么不行呢??

          # re: ActionMessages類及其綁定的組件[未登錄]  回復(fù)  更多評(píng)論   

          2007-07-04 16:17 by aa
          不行。。。

          # re: ActionMessages類及其綁定的組件  回復(fù)  更多評(píng)論   

          2007-09-17 21:41 by ee
          perfect
          主站蜘蛛池模板: 阿城市| 波密县| 四川省| 冕宁县| 白玉县| 定日县| 河东区| 朝阳市| 洛宁县| 新密市| 宜春市| 四平市| 哈巴河县| 黎平县| 天峨县| 海城市| 灌阳县| 东海县| 靖远县| 定陶县| 双柏县| 聊城市| 玛纳斯县| 阳东县| 沙坪坝区| 山阳县| 乾安县| 汶上县| 小金县| 繁峙县| 新巴尔虎右旗| 墨竹工卡县| 富阳市| 东乌珠穆沁旗| 淮阳县| 博白县| 桐柏县| 宜黄县| 土默特左旗| 池州市| 怀柔区|