java Struts 2 - 使用 <s:property> 在 <br /> 元素中转换新行 (\r\n)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4970998/
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
Struts 2 - Converting new lines (\r\n) in <br /> elements using <s:property>
提问by Mark
Is there a way to convert new lines in a <br />
HTML element using the <s:property>
tag?
有没有办法<br />
使用<s:property>
标签转换HTML 元素中的新行?
For example I have the following string value in a sampleValue
property:
例如,我在sampleValue
属性中有以下字符串值:
Hello world\r\nThis is a new line
to be rendered with <s:property value="%{sampleValue}" />
like:
以<s:property value="%{sampleValue}" />
类似的方式呈现:
Hello world<br />This is a new line
Thanks
谢谢
采纳答案by Quaternion
There is no facility in struts2 for this. If for some reason you have preformated the text, then I guess the easiest would be:
struts2 中没有这方面的功能。如果由于某种原因您预先格式化了文本,那么我想最简单的方法是:
<pre>
<s:property value="sampleValue"/>
</pre>
It is possible of course to get a regex into the display to replace "\r\n" with <br/> but there is something very ugly about that (I also find the solution above with <pre> quite ugly too but most will find that easier to understand than a regex in the middle of a jsp).
当然可以将正则表达式放入显示中以将“\r\n”替换为 <br/> 但这有一些非常难看的东西(我也发现上面带有 <pre> 的解决方案也很丑陋,但大多数会发现它比jsp中间的正则表达式更容易理解)。
Note: Instead of <pre> you can use the css property "white-space" with a value of "pre" see: http://www.w3schools.com/css/pr_text_white-space.aspfor more information on the css white space property.
注意:您可以使用值为“pre”的 css 属性“white-space”代替 <pre>,请参见:http: //www.w3schools.com/css/pr_text_white-space.asp了解有关 css 的更多信息空白属性。
回答by glasssd
Sorry for dredging this up, but now that I figured it out, I wanted a place to post the solution in case I needed it again. ;?)
很抱歉疏于处理这个问题,但现在我想通了,我想要一个地方来发布解决方案,以防我再次需要它。;?)
<s:property escapeHtml="false" value="sampleValue.replace(\"\n\", \" <br />\")" />
回答by user3173851
Yes, struts obviously escapes by default all meta-signs ... just do the following:
是的,struts 在默认情况下显然会转义所有元符号......只需执行以下操作:
<s:fielderror escape="false" />
<s:actionmessage escape="false" />
and the linebreak should work.
和换行应该工作。
回答by Jose M Rodrigues
Just try this:
试试这个:
Line of code in java file:
java文件中的代码行:
datos += rs.getString(7) + "<br/>";
Line of code in .jsp file:
.jsp 文件中的代码行:
<s:property escapeHtml="false" value="datos" />