How not to set an attribute of a component inside a composite component if it is empty?
Asked Answered
M

1

2

I have a 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 (e.g. width, height) they are rendered empty. In IE9 on win7 this causes the img tag width and height attribute to be rendered as 1. So the images have 1px width and 1px height.

Mcclure answered 2/11, 2015 at 9:56 Comment(0)
P
3

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:

Peptidase answered 2/11, 2015 at 10:7 Comment(2)
Thank you very much. But this wouldn't probably work if for example the width attribute is computed in a backing bean and the component gets rerendered/updated upon an ajax request, am I right?Mcclure
It will indeed only work if the view is implicitly/explicitly rebuilt at that point. That depends on your specific case. Just try and see if it works in your specific case. The alternative would be to override and customize the renderer.Peptidase

© 2022 - 2024 — McMap. All rights reserved.