1

I am pretty new to JSF and I am trying to make a datatable which will show some properties (first column will contain the names of the properties and the second column their values). Property types may vary (String, Long, URI,Calendar etc.).

What I want to do is, if the property type is eg. String, long, boolean, etc to output property's value as plain text (using outputText) but if it is URI or Reference(this is a custom type) to view it as a link.

This is my current JSF page (shows everything as plain text):

        <p:dataTable var="property" 
                     value="#{myBean.properties()}" >
            <p:column headerText="Property" >
                #{property}
            </p:column>

            <p:column headerText="Value">
                <h:outputText value="#{myBean.property(property)}"/>
            </p:column>
        </p:dataTable>

Thank you!

kasimoglou
  • 384
  • 3
  • 17
  • Did you try using both components with render conditions? – danRod Jul 19 '13 at 00:42
  • Can't you just simply display each property. For the link one you could something like `` ? – Andy Jul 19 '13 at 04:24
  • @danRod And write the rendered statement as: if property instance of this, that and that, render the component? Is this possible? – kasimoglou Jul 19 '13 at 09:45
  • @Andy You mean to use c:foreach and for each property to check what instance is it of, and display the corresponding component? I will try it! Thank you both for your answers! – kasimoglou Jul 19 '13 at 09:48
  • Do you have to determine the instance of each property ? and no lol I didn't mean that – Andy Jul 19 '13 at 11:22
  • @Andy Yes, whether I will use outputText or outputLink will be based on what instance is the property of (eg. String -> outputText but Resource -> outputLink).I think I have to study JSF more, it's all Chinese to me (I would say Greek, but I am Greek!). – kasimoglou Jul 19 '13 at 11:48

1 Answers1

2

Just make use of rendered attribute to conditionally render JSF components. The below example assumes that you've reworked property to be a fullworthy javabean Property with name and type properties where type is an enum containing TEXT, LINK, DATE, BOOLEAN, etc.

<p:column headerText="Value">
    <h:outputText value="#{myBean.property(property.name)}" rendered="#{property.type == 'TEXT'}" />
    <h:outputLink value="#{myBean.property(property.name)}" rendered="#{property.type == 'LINK'}">#{myBean.property(property.name)}</h:outputLink>
    <p:calendar value="#{myBean.property(property.name)}" rendered="#{property.type == 'DATE'}" />
    <p:selectBooleanCheckbox value="#{myBean.property(property.name)}" rendered="#{property.type == 'BOOLEAN'}" />
</p:column>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555