隨筆 - 3, 文章 - 0, 評(píng)論 - 12, 引用 - 0
          數(shù)據(jù)加載中……

          IoVC的組件屬性綁定----@Bind

          組件屬性綁定

              我所接觸到的基于JSF的Web應(yīng)用中,位于UI界面上的一個(gè)界面元素或組件,想要顯示數(shù)據(jù),通常的寫法是這樣:
          greeting.xhtml
          <h:inputText value="#{demo.helloduke.UserBean.name}"/>

          demo.helloduke.UserBean.java:
          @ManagedBean(name="demo.helloduke.UserBean", scope=ManagedBeanScope.SESSION)
          public class UserBean {
              
          private String name;
              
          public String getName() {
                  
          return name;
              }
              
          public void setName(String name) {
                  
          this.name = name;
              }
          }

          這樣的寫法意味著,在greeting.xhtml中,需要知道UserBean的存在,并且需要知道UserBean的name屬性是對(duì)應(yīng)著這個(gè)inputText組件的value。這在很大程度上,限制了greeting.xhtml的作用范圍,使這個(gè)UI完全沒(méi)有機(jī)會(huì)被重用。很長(zhǎng)一段時(shí)間,這都是困照著我的一個(gè)問(wèn)題。

          Binding是IoVC包含的一個(gè)重要特性,它不僅可以使UI獨(dú)立出來(lái),而且Binding是采用后期綁定模式實(shí)現(xiàn),為組件的無(wú)狀態(tài)的實(shí)現(xiàn)提供了底層基礎(chǔ)支撐。關(guān)于IoVC的實(shí)現(xiàn)方式,在這篇隨筆中并不過(guò)多討論。
          在IoVC的編程模式下,對(duì)UI上的界面元素的控制力被轉(zhuǎn)移到了ManagedBean中,UI不需要關(guān)心誰(shuí)在使用它,以及數(shù)據(jù)的來(lái)源。
          IoVC模式的Hello Duke的UI及managedBean:
          greeting.xhtml
          <h:inputText id="name"/>

          demo.helloduke.UserBean.java:
          @ManagedBean(name="demo.helloduke.UserBean", scope=ManagedBeanScope.SESSION)
          public class UserBean {
              @Bind
              
          private String name;
          }

          @Bind標(biāo)簽,提供了將ManagedBean的filed綁定到組件的attributes中的能力,默認(rèn)情況下,@Bind根據(jù)所作用的filed的name來(lái)匹配UI中的元素的id,然后將field的值取出綁定到對(duì)應(yīng)的UI中的組件上,上述代碼表示,將UserBean中的name屬性,綁定到與之對(duì)應(yīng)的inputText組件的value中。

          下面列出了@Bind標(biāo)簽的典型使用場(chǎng)景:

              @Bind
              
          private UIDataGrid grid;

              @Bind(id
          ="grid", attribute="width")
              
          private int width;

              @Bind(id
          ="grid", attribute="height")
              
          private int height;

              
          public void initGrid() {
                  
          this.width = 500;
                  
          this.height = 400;
              }

              
          public void reload() {
                  
          this.grid.reload();
              }

          使用@Bind標(biāo)簽將組件的attributes與ManagedBean中的屬性一一綁定起來(lái)之后,在UI上如果組件發(fā)生變化,其變化了的屬性會(huì)反映到ManagedBean中,同樣,在ManagedBean中,如果更改了屬性值,UI組件也會(huì)發(fā)生相應(yīng)的變化。
          一個(gè)使用IoVC模式的完整頁(yè)面與ManagedBean(其中涉及到的其他標(biāo)簽,會(huì)在后面介紹到):
          運(yùn)行效果:


          calc.xhtml
          <?xml version="1.0" encoding="UTF-8"?>
          <f:view xmlns:f="http://java.sun.com/jsf/core"
                  xmlns
          ="http://www.w3.org/1999/xhtml"
                  xmlns:h
          ="http://java.sun.com/jsf/html"
                  xmlns:w
          ="http://www.apusic.com/jsf/widget"
                  renderKitId="AJAX">
            <w:page title="View Binding Example">
            
          <h:form>
              
          <h:panelGrid columns="1">
                
          <h:inputText id="first"/>
                
          <h:inputText id="second"/>
                
          <h:outputText id="result"/>
              
          </h:panelGrid>
              
          <h:commandButton value="+" id="add"/>
              
          <h:commandButton value="-" id="subtract"/>
              
          <h:commandButton value="*" id="multiply"/>
              
          <h:commandButton value="/" id="divide"/>
            
          </h:form>
            
          <h:messages/>
            
          </w:page>
          </f:view>
          CalcBean.java:
          @ManagedBean(scope=ManagedBeanScope.SESSION)
          public class CalcBean
          {
              @Bind
              private double first = 10;

              @Bind
              private double second = 20;

              @Bind
              private double result;

              @Bind(id
          ="result", attribute="style")
              
          private String style;

              @Action
              
          public void add() {
                  result 
          = first + second;
                  style 
          = "color:red";
              }

              @Action
              
          public void subtract() {
                  result 
          = first - second;
                  style 
          = "color:green";
              }

              @Action
              
          public void multiply() {
                  result 
          = first * second;
                  style 
          = "color:blue";
              }

              @Action
              
          public void divide() {
                  result 
          = first / second;
                  style 
          = "color:black";
              }
          }




          posted on 2008-02-24 23:03 張旭 閱讀(1356) 評(píng)論(6)  編輯  收藏

          評(píng)論

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          呵呵,看起來(lái)很簡(jiǎn)潔的樣子,不錯(cuò)
          2008-02-24 23:59 | javafan

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          所謂的ManagedBean不就是一般的view object?像bind這樣的功能是很靈活,但是我倒是認(rèn)為約定比可配置更有價(jià)值,本來(lái)ui就是個(gè)經(jīng)常變動(dòng)的地方。
          2008-02-25 10:03 | dennis

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          原來(lái)是 Apusic OperaMasks 里面的東西。。。
          2008-02-25 10:11 | BeanSoft

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          想法不錯(cuò),有綁定嫌疑哦
          2008-02-25 11:39 | 試試

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          這是operamask新版本的東西嗎?
          請(qǐng)問(wèn)樓上的說(shuō)他有綁定嫌疑是什么意思呢?
          2008-02-25 21:00 | 回復(fù)

          # re: IoVC的組件屬性綁定----@Bind  回復(fù)  更多評(píng)論   

          如何保證一一對(duì)應(yīng)呢?
          在這里我沒(méi)看出來(lái)呀。

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新乡县| 汤阴县| 马龙县| 和平县| 桦川县| 六盘水市| 彭泽县| 沾益县| 青河县| 泰州市| 宝坻区| 苏尼特右旗| 朝阳市| 班玛县| 佛坪县| 蓝田县| 西宁市| 望谟县| 察隅县| 中江县| 东乡县| 涿州市| 那曲县| 利津县| 北辰区| 古丈县| 克山县| 边坝县| 青浦区| 区。| 琼结县| 永嘉县| 桑植县| 股票| 伊春市| 思南县| 沙河市| 开封县| 额尔古纳市| 乌审旗| 衡山县|