How not to set a component attribute inside a composite component if it is empty?

I have h: graphicImage in a composite component like this:

<composite:interface> <composite:attribute name="name" required="true" type="java.lang.String" /> <composite:attribute name="alt" required="false" type="java.lang.String" /> <composite:attribute name="height" required="false" type="java.lang.String" /> <composite:attribute name="width" required="false" type="java.lang.String" /> </composite:interface> <composite:implementation> <h:graphicImage url="something-common#{cc.attrs.name}" alt="#{cc.attrs.alt}" height="#{cc.attrs.height}" width="#{cc.attrs.width}" /> </composite:implementation> 

This works, however, if some attributes are not set (for example, width, height), they are displayed empty. In IE9 on win7, this causes the width and height attribute of the img tag to display as 1. Thus, the images are 1px wide and 1px high.

+1
jsf attributes composite-component conditional-rendering
source share
1 answer

You can conditionally add attributes via <c:if><f:attribute> .

 <h:graphicImage ...> <c:if test="#{not empty cc.attrs.height}"><f:attribute name="height" value="#{cc.attrs.height}" /></c:if> <c:if test="#{not empty cc.attrs.width}"><f:attribute name="width" value="#{cc.attrs.width}" /></c:if> </h:graphicImage> 

See also:

  • JSTL in JSF2 Facelets ... makes sense?
+2
source share

All Articles