创建了一个标签库描述符文件:
<?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>
图像标签处理程序
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);
}
}
}
图像组件
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;
}
}
图像渲染器
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");
}
}
访问标签库的代码是
<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>
bean 包含多张照片,当页面显示时,这些照片必须以列数显示。我面临的问题是它不能向我显示多张图片给出了一个错误说
根据标记文件中的 TLD 或属性指令,属性绑定不接受任何表达式
如果 bean 包含单个 Image 它显示的代码是
<tr:panelHeader id="panelHeader" styleClass="banner" text="">
<bsil:BsilImage url="${LandingBean.bannerImage}"
style="bannerImage" type="banner"></bsil:BsilImage>
</tr:panelHeader>
我想知道如何使用自定义组件显示多个图像。