如果复合组件为空,如何不设置组件的属性? [英] How not to set an attribute of a component inside a composite component if it is empty?

查看:119
本文介绍了如果复合组件为空,如何不设置组件的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的复合组件中有一个h:graphicImage:

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>

这有效,但是,如果未设置某些属性(例如,宽度,高度),则将它们设置为空.在win7的IE9中,这会使img标签的width和height属性呈现为1.因此图像的宽度为1px,高度为1px.

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.

推荐答案

您可以通过<c:if><f:attribute>有条件地添加属性.

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>

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 这篇关于如果复合组件为空,如何不设置组件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆