How to display a line break with outputText?

笑着哭i 提交于 2019-12-03 02:36:46

问题


I need to render a line break using outputText so that I can utilize the rendered attributed. I tried

<h:outputText value="<br/>" escape="false" />

but it generated exception

The value of attribute "value" associated with an element type "null" must not contain the '<' character. 

回答1:


That's indeed not valid since Facelets because it's syntactically invalid in XML. You'd need to manually escape the XML special characters like <, > and so on.

<h:outputText value="&lt;br/&gt;" escape="false" />

You can however just emit the <br/> in template text without the need for a <h:outputText>.

<br/>

To render it conditionally, wrap it in for example a <ui:fragment>.

<ui:fragment rendered="#{bean.rendered}"><br /></ui:fragment>

A <h:panelGroup> is also valid as it doesn't emit anything to the HTML anyway.

<h:panelGroup rendered="#{bean.rendered}"><br /></h:panelGroup>



回答2:


JSF PAGE

<h:outputText value="#{car.crg}" escape="false" style="white-space: pre-wrap;word-wrap: break-word; " />

escape should be false and write the bean Getter method as follows

 public String getCrg() {
         return crg.replace("<br/>", "&lt;br /&gt;");
        //return crg;
    }



回答3:


You can try putting the "<br />" inside a resource bundle and then get the value from that resource bundle.



来源:https://stackoverflow.com/questions/3337331/how-to-display-a-line-break-with-outputtext

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