2010-01-06 29 views
0

創建一個標籤庫描述符文件:JSF自定義圖像組件問題,以顯示多個圖像

<?xml version="1.0" encoding="UTF-8"?> 
    <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> 
     <tlib-version>1.0</tlib-version> 
     <short-name>image</short-name> 
     <uri>/WEB-INF/tlds/image</uri> 
     <tag> 
     <name>BsilImage</name> 
     <tag-class>com.custom.image.ImageTagHandler</tag-class> 
     <body-content>scriptless</body-content> 
     <attribute> 
      <name>url</name> 
      <required>true</required> 
      <rtexprvalue>true</rtexprvalue> 
      <type>java.lang.String</type> 
     </attribute> 
     <attribute> 
      <name>style</name> 
      <rtexprvalue>true</rtexprvalue> 
      <type>java.lang.String</type> 
     </attribute> 
     <attribute> 
      <name>type</name> 
      <required>true</required> 
      <rtexprvalue>true</rtexprvalue> 
      <type>java.lang.String</type> 
     </attribute> 
     </tag> 
    </taglib> 

ImageTagHandler

public class ImageTagHandler extends UIComponentELTag { 

    private String url; 
    private String style; 
    private String type; 
    private static final String IMAGE_COMP_TYPE = "IMAGE_COMPONENT"; 
    private static final String IMAGE_RENDERER_TYPE = "IMAGE_RENDERER"; 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public void setStyle(String style) { 
     this.style = style; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getComponentType() { 
     return IMAGE_COMP_TYPE; 
    } 

    public String getRendererType() { 
     return IMAGE_RENDERER_TYPE; 
    } 

    @Override 
    protected void setProperties(UIComponent component) { 
     super.setProperties(component); 
     ImageComponent imgComponent = 
       ((ImageComponent) component); 
     if (type != null) { 
      imgComponent.setType(ImageTranscoder.getImageTypeOf(type)); 
     } 
     if (url != null) { 
      imgComponent.setUrl(ImageTranscoder.getImagePath(url)); 
     } 

     if (style != null) { 
      imgComponent.setStyle(style); 
     }   
    } 
} 

ImageComponent

public class ImageComponent extends UIOutput { 

    private String url; 
    private String style; 
    private String type; 
    private static final String IMAGE_COMP_FAMILY = "IMAGE_FAMILY"; 

    @Override 
    public String getFamily() { 
     return IMAGE_COMP_FAMILY; 
    } 

    /** 
    * Get the value of type 
    * 
    * @return the value of type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * Set the value of type 
    * 
    * @param type new value of type 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * Get the value of style 
    * 
    * @return the value of style 
    */ 
    public String getStyle() { 
     return style; 
    } 

    /** 
    * Set the value of style 
    * 
    * @param style new value of style 
    */ 
    public void setStyle(String style) { 
     this.style = style; 
    } 

    /** 
    * Get the value of url 
    * 
    * @return the value of url 
    */ 
    public String getUrl() { 
     return url; 
    } 

    /** 
    * Set the value of url 
    * 
    * @param url new value of url 
    */ 
    public void setUrl(String url) { 
     this.url = url; 
    } 

} 

ImageRenderer

public class ImageRenderer extends Renderer { 
    @Override 
    public void encodeBegin(FacesContext context, UIComponent component) 
        throws IOException { 
     ImageComponent imgComponent = (ImageComponent)component; 
     ResponseWriter writer = context.getResponseWriter(); 
     writer.startElement("div",component); 
     writer.startElement("img", imgComponent); 
     writer.writeAttribute("src",imgComponent.getUrl(), "url"); 
     writer.writeAttribute("class", imgComponent.getStyle(), "style"); 
     writer.endElement("div"); 
    } 

} 

代碼來訪問TAG LIB IS

<h:dataTable var="landing" binding="${LandingBean.tourPackages}" > 
     <h:column> 
      <tr:panelGroupLayout layout="vertical" styleClass="landingListing"> 
       <bsil:BsilImage style="listingImage" url="${landing.photoThumbnail}" type="banner"/> 
      </tr:panelGroupLayout> 
     </h:column> 
</h:dataTable> 

的豆包含的多張照片,其具有在柱的數目將被所示顯示頁面時。我現在面臨的問題是它的傾斜向我展示多張圖片給出了一個錯誤說

根據TLD或屬性的標籤文件 指令,屬性 綁定不接受任何 表達

如果豆包含單個圖像它顯示的代碼是

<tr:panelHeader id="panelHeader" styleClass="banner" text=""> 
        <bsil:BsilImage url="${LandingBean.bannerImage}" 
         style="bannerImage" type="banner"></bsil:BsilImage> 
       </tr:panelHeader> 

我想知道如何使用自定義組件顯示多個圖像。

回答

0

根據您引用的錯誤消息,似乎問題出在<h:dataTable ...>標籤中,似乎稱爲「綁定」的屬性顯然由於某種原因而不接受表達式。

E.g.

<tag> 

    <name>dataTable</name> 

    <attribute> 
     <name>binding</name> 
     <!-- add this: --> 
     <rtexprvalue>true</rtexprvalue> 
    </attribute> 

</tag> 

如果你擁有dataTable標記的代碼,接下來添加<rtexprvalue>true</rtexprvalue>到該標籤的「綁定」屬性(就像你在你的形象標籤一樣)。如果你不這樣做,請參閱它的文檔,找出它不允許表達式的原因。