Insert a line break inside

2020-06-04 03:11发布

问题:

Basically I am simply trying to add a <br> (or something equivalent) to the "value" attribute of a <p:commandButton> like this:

<p:commandButton value="#{aBean.text}" />  
<!-- #{aBean.text} returns for example "text1<br>text2" -->

Sadly, there is no possibility to set escape="false". Trying to add a custom converter didn't work, either. I have, without success, also tried to do it like this:

<p:commandButton>  
     <h:outputText escape="false" value="#{aBean.text}" />  
</p:commandButton>

In my opinion adding a simple line break should be easy enough, right? Does anyone have a solution for this?

回答1:

You need to use &#10; which represents the XML entity reference for \n.

<p:commandButton value="text1&#10;text2" style="white-space: pre;" />

The white-space: pre; is mandatory on <p:commandButton>, but not on <h:commandButton>, because the PrimeFaces one generates it as a <button type="submit"><span> instead of a <input type="submit">.