Replace spaces with nonbreakable spaces in JSF [duplicate]

不羁岁月 提交于 2019-12-11 10:55:41

问题


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&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp8.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!