简体   繁体   中英

Conditionally render plain HTML attribute in Facelets

I'd like to conditionally render attributes of a plain HTML <div> element. I tried as below using <c:if> :

<ui:composition
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:c="http://java.sun.com/jstl/core"
        xmlns:f="http://java.sun.com/jsf/core" 
        xmlns:h="http://java.sun.com/jsf/html">        

    <div data-product-id="#{ID}" 
        <c:if test="${not empty testvalue1}">
            data-test1-for="#{testvalue1}"
        </c:if>
        <c:if test="${not empty testvalue2}">
            data-test2-for="#{testvalue1}"
        </c:if>
    >
        div content
    </div>
</ui:composition>

However, it produced a Facelet exception:

com.sun.facelets.FaceletException: Error Parsing /assets/template/module/container/product/product-marker.xhtml: Error Traced[line: 9] Element type "div" must be followed by either attribute specifications, ">" or "/>"

How can I conditionally render attributes of a plain HTML <div> element?

You don't need those if statements. If the values are empty, attributes won't be rendered on the page automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM