1

On a page created with use of JSF, JSTL and Trinidad, I have some tag called with many attributes.

<tr:selectBooleanRadio
        group="#{groupId}"
        id="#{groupId}_#{Id}_radio"
        selected="#{value.yesterday}"
        text="#{msg.ib_selectTimeIntervalRadio_yesterday_label}"
        shortDesc="#{ib:fn_coalesce(title,label)}"
        simple="#{ib:fn_coalesce(simple,true)}"
        styleClass="#{ib:fn_coalesce(styleClass,'selectTimeIntervalRadio')}"/>

I want to show it in <td> element or directly, conditionally. Now I do it as:

<c:when test="#{not horizontal}">
    <tr>
        <td>
            <tr....>
        </td>
    </tr>
</c:when>
<c:when test="#{horizontal}">
    <tr...>
</c:when>

This way I repeat the same piece of code twice, which is obviously bad. What can I do?

IMHO, it is useless to create a new custom component for the tr... call - calling of that will have all the same problems.

Maybe, I can envelope one tag in another conditionally? Can I?

The other way could be such: put the tr... tag in some sort of variable and use the same when construction with this variable. Maybe, it is possible?

Do you know another way how to escape this code duplication?

Gangnus
  • 24,044
  • 16
  • 90
  • 149

1 Answers1

2

If you're using legacy JSP, wrap it in <f:verbatim>.

<f:verbatim rendered="#{not horizontal}"><tr><td></f:verbatim>
    <tr:selectBooleanRadio ... />
<f:verbatim rendered="#{not horizontal}"></td></tr></f:verbatim>

If you're using JSPX or Facelets, which would produce a XML error on above construct which is illegal in XML, escape it in a <h:outputText escape="false">.

<h:outputText value="&lt;tr&gt;&lt;td&gt;" escape="false" rendered="#{not horizontal}" />
    <tr:selectBooleanRadio ... />
<h:outputText value="&lt;/td&gt;&lt;/tr&gt;" escape="false" rendered="#{not horizontal}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • For the last I need to declare xmlns:h="http://java.sun.com/jsf/html". Looks dirty. The first variant is much more sympathetic, what have I delare for this f:, please ? – Gangnus Jan 13 '16 at 14:38
  • As answered, it won't work in Facelets. It's just standard core tag, by the way, only deprecated since JSF 2.0. – BalusC Jan 13 '16 at 14:40
  • I have already checked :-(. And are there some more clean solution? Something as envelop in element on condition? – Gangnus Jan 13 '16 at 14:43
  • It violates XML rules (document MUST be well formed). Hence ugliness. Only clean alternative is custom component with renderer. – BalusC Jan 13 '16 at 14:44
  • But that violates the much more basic rule of no duplications. – Gangnus Jan 13 '16 at 14:49