Struts 2 - Converting new lines (\r\n) in <br /> elements using <s:property>
Is there a 开发者_C百科way to convert new lines in a <br />
HTML element using the <s:property>
tag?
For example I have the following string value in a sampleValue
property:
Hello world\r\nThis is a new line
to be rendered with <s:property value="%{sampleValue}" />
like:
Hello world<br />This is a new line
Thanks
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 />\")" />
There is no facility in struts2 for this. If for some reason you have preformated the text, then I guess the easiest would be:
<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).
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.asp for more information on the css white space property.
Yes, struts obviously escapes by default all meta-signs ... just do the following:
<s:fielderror escape="false" />
<s:actionmessage escape="false" />
and the linebreak should work.
Just try this:
Line of code in java file:
datos += rs.getString(7) + "<br/>";
Line of code in .jsp file:
<s:property escapeHtml="false" value="datos" />
精彩评论