Html 如何在 <h:outputText /> 值中添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22088634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:04:41  来源:igfitidea点击:

How to add break line in <h:outputText /> value?

htmljsfprimefaces

提问by Zin Win Htet

I'm trying break line in <h:outputText />value.

我正在尝试打破<h:outputText />价值线。

I simply put a <br>in my value as HTML theory. But it doesn't work and a page error show up.

我只是把<br>我的价值作为 HTML 理论。但它不起作用并显示页面错误。

When I removed the <br/>. It works again properly.

当我删除<br/>. 它再次正常工作。

My code is:

我的代码是:

<h:outputText value="[#{order.product.code}] #{order.product.name} <br/> #{order.qty}"/>

I tried another way.

我尝试了另一种方式。

<h:outputText value="[#{order.product.code}] #{order.product.name} &#10 #{order.qty}"/>

No change at all.

根本没有变化。

回答by Wanna Coffee

Use &lt;for <& &gt;for >, then make your html tag.

使用&lt;for <& &gt;for >,然后制作你的 html 标签。



Like:

喜欢:

&lt;br /&gt; - For <br />

Then you have to tell browser escape html tag, placeescape= "true"attirbute inside <h:outputText />.

然后你必须告诉浏览器转义 html 标签,将escape= "true"attirbute 放在里面<h:outputText />



According to your requirement try this,

根据您的要求试试这个,

Sample code:

示例代码:

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

Output:

输出:

value-one 
value-two 
value-three

回答by BalusC

Why exactly do you need <h:outputText>? You don't seem to taking benefit of any of its additional features on top of plain EL in template text.

为什么你需要<h:outputText>?您似乎没有在模板文本中的纯 EL 之上利用其任何附加功能。

Just get rid of it and write it like so:

只需摆脱它并像这样写:

[#{order.product.code}] #{order.product.name} <br/> #{order.qty}

Note that this is not possible in legacy JSP, but given that you're using PrimeFaces, which doesn't have a JSP compatible tag library (JSP is deprecated >4 years ago), then you should surely be using Facelets.

请注意,这在遗留 JSP 中是不可能的,但考虑到您使用的是 PrimeFaces,它没有与 JSP 兼容的标记库(JSP 在 4 年前已被弃用),那么您肯定应该使用 Facelets。

回答by hurleytom

You can use multiple h:outputTextags with < br />tags inbetween. Try the following:

您可以使用多个h:outputTex标签,< br />中间有标签。请尝试以下操作:

<h:outputText value="[#{order.product.code}]"/>
<br/>
<h:outputText value="#{order.product.name}"/>
<br/> 
<h:outputText value="#{order.qty}"/>

UPDATE:

更新:

Or use escape="false":

或使用escape="false"

<h:outputText value="[#{order.product.code}]&lt;br /&gt;#{order.product.name}&lt;br /&gt;#{order.qty}" escape="false" />

回答by Vasil Lukach

Add attribute escape="false"in h:outputText. If your valuecontains <br />then you will have break line in text.

添加属性escape="false"h:outputText。如果您value包含,<br />那么您将在文本中有断行。

回答by Zin Win Htet

Thank you so much everyone..! Especially @Wanna coffee and @hurleytom. Your codes really work.

谢谢大家!谢谢..!尤其是@Wanna 咖啡和@hurleytom。您的代码确实有效。

<h:outputText value="[#{order.product.code}]&lt;br /&gt;#{order.product.name}&lt;br /&gt;#{order.qty}" escape="false" />