问题
I need some JSF 2.1.29 advise. I have the following using of the bean's property:
#{someBean.someProperty}
Where #{someBean.someProperty} returns 7 8 (note the number of spaces between the digits). And in the browser it's displayed as just 7 8. When I replace those spaces with , then they are displayed as is:
7 8
How can I get to actually display as 7 8?
回答1:
That is HTML escaping at work, which is enabled by default. Disable it using the escape attribute:
<h:outputText escape="false" value="#{backingBean.someText}"/>
I just tested this, and it outputs 7 8 when the backing bean returns 7      8.
Alternatively, use the CSS white-space property to preserve plain (not non-breaking) white spaces:
<h:outputText value="#{backingBean.someText}" style="white-space: pre"/>
The CSS solution is often better, as it maintains XSS protection.
See also
- JSF 2.0 component to interpret String with HTML code
- Display element as preformatted text via CSS
来源:https://stackoverflow.com/questions/30239497/replace-spaces-with-nonbreakable-spaces-in-jsf