如何将 <br /> 标签插入到 Java 字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008250/
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
How to insert <br /> tags into a java String
提问by april26
I am trying to insert line break tags
into some text and displaying it on a web page. The < and > signs are being translated into <
and >
and the tags are showing up as text on the web page.
我正在尝试将换行标记
插入一些文本并将其显示在网页上。< 和 > 符号被翻译成<
and>
标签在网页上显示为文本。
The text looks like this when I select it from the database (I've output it to SYSOUT):
当我从数据库中选择它时,文本看起来像这样(我已经将它输出到 SYSOUT):
version 12.4
service timestamps debug datetime
service timestamps log datetime
service password-encryption
Then I run it through this little filter:
然后我通过这个小过滤器运行它:
public DevConfigs getDevConfig() {
String config = devConfig.getConfig();
Pattern pattern = Pattern.compile(".$", Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(config);
String newConfig = matcher.replaceAll("<br />");
devConfig.setConfig(newConfig);
return this.devConfig;
}
Here is the web page (it's a Seam application using facelets):
这是网页(它是一个使用 facelets 的 Seam 应用程序):
<rich:tab label="Config">
hello<br />
there<br />
#{devConfig.config}
</rich:tab>
And the page source looks like this:
页面源如下所示:
hello<br />
there<br />
<br />
<br />
version 12.<br />
service timestamps debug datetim<br />
service timestamps log datetim<br />
service password-encryptio<br />
<br />
As you can see, my tag comes out as HTML characters and not as tags. What do I need to do to insert line break tags at the end of each line of text??
如您所见,我的标签是作为 HTML 字符而不是标签出现的。我需要做什么才能在每行文本的末尾插入换行符?
TDR
TDR
回答by George Profenza
I don't write that much Java, especially nothing related to this, but I'm wondering, can this
我没有写那么多 Java,尤其是与此无关,但我想知道,这可以吗?
<rich:tab label="Config">
hello<br />
there<br />
#{devConfig.config}
</rich:tab>
not be written as this
不能这样写
<rich:tab label="Config">
<![CDATA[
hello<br />
there<br />
]]>
#{devConfig.config}
</rich:tab>
or simply:
或者干脆:
<rich:tab label="Config">
<![CDATA[
hello
there
]]>
#{devConfig.config}
</rich:tab>
?
?
回答by BalusC
You need to disable the (default) HTML escaping of text. You can do that with h:outputText
(or any RichFaces equivalent for that) with the escape
attribute set to false
.
您需要禁用文本的(默认)HTML 转义。您可以使用h:outputText
(或任何等效的 RichFaces)将escape
属性设置为false
.
<h:outputText value="#{bean.property}" escape="false" />
回答by Martijn Courteaux
Maybe I didn't understand your question completely. But isn't it possible to replace simply the \n
with <br />
?
也许我没有完全理解你的问题。但是不能简单地替换\n
with<br />
吗?
public DevConfigs getDevConfig() {
String config = devConfig.getConfig();
String newConfig = config.replace("\n", "<br />");
devConfig.setConfig(newConfig);
return this.devConfig;
}
I entered your text in this regex tester. This works fine, but if I choose Preg
by Dialect
... He replaces with <br />
. Only with Javascript dialect it goes correct.
So maybe that is the same problem.
我在这个正则表达式测试器中输入了您的文本。这工作得很好,但如果让我选择Preg
的Dialect
......他替换<br />
。只有使用 Javascript 方言它才能正确。
所以也许这是同样的问题。
Oh. When you use .$
for your regex, you remove the last char from the line (when replacing).
哦。当您.$
用于正则表达式时,您从行中删除最后一个字符(替换时)。
回答by Aleja_Vigo
As it has been said before, its all about the view. You can't do anything just in the controller/model or java String. No magic escaping or combination can make the <br>
get to the generated HTML unharmed. You have to find the required attribute change in your JSP.
正如之前所说,一切都与视图有关。你不能只在控制器/模型或 java 字符串中做任何事情。没有魔法转义或组合可以使<br>
生成的 HTML 不受损害。您必须在 JSP 中找到所需的属性更改。
In Struts is the "filter"one, put it false to avoid the escaping of your HTML:
在 Struts 中是“过滤器”之一,将其设置为 false 以避免转义您的 HTML:
Java
爪哇
String text = "This line will <br> break";
JSP
JSP
<bean:write name="object" property="attribute" filter="false"/>