1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 1
    How many spaces are there in the real source of the html on the browser side? Multiple or 1? Try http://stackoverflow.com/questions/9753597/display-element-as-preformatted-text-via-css – Kukeltje May 15 '15 at 01:53

1 Answers1

2

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

DavidS
  • 5,022
  • 2
  • 28
  • 55
  • I'm not certain, @BalusC. He says "Actually, I _would_ replace those space with &nbsp *but* they are rendered by JSF." If you're right, OP just needs to use `String.replace` in the backing-bean. I hope OP will clarify the problem. – DavidS May 15 '15 at 00:31
  • @BalusC No, The answer is perfectly correct. I wasn't aware about it. – St.Antario May 15 '15 at 04:53
  • @BalusC BTW, the `` tag doesn;t have such an attribute. Is there a quick solution for that case? –  May 15 '15 at 06:47
  • 1
    It can be done with just CSS without need for non breaking spaces. – BalusC May 15 '15 at 10:33