爪哇一角

          共同探討STRUTS#HIBERNATE#SPRING#EJB等技術
          posts - 3, comments - 6, trackbacks - 0, articles - 99
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          十一 自定義組件標簽

          Posted on 2009-01-20 14:50 非洲小白臉 閱讀(229) 評論(0)  編輯  收藏 所屬分類: JSF

          完成了UIComponent的定制后,下面就需要自定義一個Tag來與之對應。自定義Tag的目的在于設定UIComponent的屬性,取得Renderer的值等。

          自定義Tag需要繼承UIComponentTag

          package test;

          import javax.faces.application.Application;

          import javax.faces.component.UIComponent;

          import javax.faces.context.FacesContext;

          import javax.faces.el.ValueBinding;

          import javax.faces.webapp.UIComponentTag;

          繼承了UIComponentTag

           
          public class TextWithCmdTag extends UIComponentTag {

              private String size;

              private String value;

          綁定到自定義的Component組件。

           
              public String getComponentType() {

                  return "test.TextWithCmd";

              }

          綁定到Renderer。這里沒有設定,因為這個組件沒有用Renderer編解碼,而是Component自己實現的。

           
              public String getRendererType() {

                  return null;

              }

              public void setProperties(UIComponent component) {

                  super.setProperties(component);

                

                  setStringProperty(component, "size", size);

                  setStringProperty(component, "value", value);

              }

              private void setStringProperty(UIComponent component,

                                 String attrName, String attrValue) {

                  if(attrValue == null)

                      return;

          設置Tag的值綁定到Component上。這樣,Component取值的時候,如果元件屬性設定值找不到,就會從綁定值中去尋找。

           
                  if(isValueReference(attrValue)) {

                      FacesContext context =

                                   FacesContext.getCurrentInstance();

                      Application application =

                                   context.getApplication();

                      ValueBinding binding =

                             application.createValueBinding(attrValue);

                      component.setValueBinding(attrName, binding);

                  }

                  else {

                      component.getAttributes().

                                    put(attrName, attrValue);

                  }

              }

              public void release() {

                  super.release();

                  size = null;

                  value = null;

              }

              public String getSize() {

                  return size;

              }

              public void setSize(String size) {

                  this.size = size;

              }

              public String getValue() {

                  return value;

              }

              public void setValue(String value) {

                  this.value = value;

              }

           }

          ▲綁定到Tag上的Conponent組件用的是faces-config.xml中定義的別名,所以我們隨時可以替換掉對于的Component而不改變Tag代碼。

          自定義Tag需要在Tld中聲明:

          <tag>

               <name>textcmd</name>

               <tag-class>test.TextWithCmdTag</tag-class>

               <body-content>empty</body-content>

               <attribute>

                  <name>size</name>

               </attribute>

               <attribute>

                  <name>value</name>

                  <required>true</required>

               </attribute>

           </tag>

          具體程序參看示例程序。

           

          新建一個JSP和一個綁定好的javaBean,就可以看到運行結果了。

          對這類jspBean的建立,前面已經有很多例子了,這里就不再說了。

          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

          <%@ taglib uri="/WEB-INF/textcmd.tld" prefix="oc"%>

          <html>

          <link href="../../styles.css" rel="stylesheet" type="text/css" />

          <head>

          <title></title>

          </head>

          <body>

          <f:view>

                 <h:form>

               Input data: <oc:textcmd size="10" value="#{componentTestBean.data}" />

                 </h:form>

                 <h:outputText value="#{componentTestBean.data}" />

          </f:view>

          </body>

          </html>

          運行地址:http://localhost:8080/jsfTest/pages/componentTest.faces

          具體程序參看示例程序。

           

          主站蜘蛛池模板: 天柱县| 大同县| 西峡县| 武川县| 平谷区| 砀山县| 永吉县| 望奎县| 德保县| 汽车| 沙湾县| 莲花县| 东乡族自治县| 泽州县| 邵阳市| 通江县| 隆昌县| 遵义县| 通城县| 华坪县| 桂平市| 三门县| 新平| 八宿县| 永州市| 永兴县| 兰溪市| 改则县| 怀宁县| 察哈| 襄樊市| 彰化县| 从化市| 乐都县| 淮安市| 上林县| 博乐市| 徐水县| 罗山县| 丰顺县| 潼南县|