2

Is it possible to get HTML code that is inside a bean variable and print it in the Facelets file as HTML code and not as text?

Example:

myBean.java

public String getMyHtmlCode(){
    return "<span class="little">Internet</span> 100Mb <span class="little">+television</span>"
}

file.xhtml

 <h2>#{myBean.myHtmlCode}</h2>

But in the navigator appears

<span class="little">Internet</span> 100Mb <span class="little">+television</span>

And is not interpreted as html.

Any idea?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dcalap
  • 1,048
  • 2
  • 13
  • 37
  • 1
    Would something like this help? http://stackoverflow.com/questions/11020438/backing-bean-property-that-should-return-a-string-with-html-code-returns-empty-s – Peter Jaloveczki Jun 11 '13 at 14:40
  • @PeterJaloveczki yes, that's my answer as well :). – Luiggi Mendoza Jun 11 '13 at 14:40
  • possible duplicate of [How do I return HTML from managed bean in JSF?](http://stackoverflow.com/questions/2351639/how-do-i-return-html-from-managed-bean-in-jsf) – BalusC Jun 11 '13 at 14:43

1 Answers1

2

In BalusC words from his comment:

(You) should just disable the default escaping of the text

<h2>
    <h:outputText value="#{myBean.myHtmlCode}" escape="false" />
</h2>

Just note that this code is prone to XSS attacks.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 2
    No, he should not escape the text. He should just disable the default escaping of the text. – BalusC Jun 11 '13 at 14:42