0

How to display a custom property using facelet expression language?

For example:

<h:outputText value="#{contact.customTypeProperty}" />

where customTypeProperty is of type CustomClass, and I want to display the String returned by its toString()?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
anergy
  • 1,374
  • 2
  • 13
  • 29

1 Answers1

1

That should already be the default behaviour. You don't need to change anything on the given code example, assuming that the toString() method is properly implemented on the CustomClass. However, if it returns HTML, you'd need to add escape="false" to the output text to prevent JSF from auto-escaping it (which it does in order to prevent XSS attacks on user-controlled input):

<h:outputText value="#{contact.customTypeProperty}" escape="false" />

This is however not necessarily the best practice. You should control the presentation in the view side, not in a toString() in the model side. For example, assuming that CustomClass has in turn two properties foo and bar and you'd like to present it in a table:

<h:panelGrid columns="2">
    <h:outputText value="Foo" />
    <h:outputText value="#{contact.customTypeProperty.foo}" />

    <h:outputText value="Bar" />
    <h:outputText value="#{contact.customTypeProperty.bar}" />
</h:panelGrid>

If you did this to avoid code repetition, then you should actually be using an include file or a tag file. See also When to use <ui:include>, tag files, composite components and/or custom components?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC, somehow the Object's toString is called. Although it is correctly overridden in CustomClass. – anergy Nov 22 '11 at 17:15
  • Put `@Override` annotation on the `toString()` method to make sure that you didn't make any typos of changes in method signature. It has to be `public String toString() {}` without arguments and throws. – BalusC Nov 22 '11 at 17:22
  • Override notation is there, I guess the issue is unrelated to JSF. The wsimport doesn't generate toString. So the problem lies somewhere there. – anergy Nov 22 '11 at 17:27