使用c出标签时不会出现新行 [英] new line doesn't appear when using c out tag
问题描述
我将\ n附加到字符串中,并且在使用s标签textarea时,新行已被追加并且数据逐行显示。但是当我使用c标签时,数据显示在一行中。如何使用c out标签逐行显示?
I appended "\n" to the String and when using s tag textarea, the newline has been appended and data are shown line by line. But when I use c out tag, data are shown in one line. How can I show line by line using with c out tag?
StringBuffer sb = new StringBuffer();
for (MyBean bean : beanList) {
sb.append((bean.getName());
sb.append("\n");
}
return sb.toString();
JSP
<c:out value="${myData}"/>
推荐答案
JSP产生HTML,在HTML中,新行将由< br>
元素,而不是换行字符,更重要的是,如果您查看一般的HTML源代码,您会看到很多换行符,但默认情况下它们根本不会被webbrowser解释。
JSP produces HTML. In HTML, new lines are to be represented by the <br>
element, not by the linefeed character. Even more, if you look in the average HTML source, you'll see a lot of linefeed characters, but they are by default not interpreted by the webbrowser at all.
除了使用HTML < br>
元素而不是换行字符外,
Apart from using the HTML <br>
element instead of the linefeed character,
sb.append("<br />");
,然后在没有< c:out>
的情况下打印,例如 $ {myData}
,你也可以使用HTML < pre>
元素保留空白,
and printing it without <c:out>
like so ${myData}
, you can also use the HTML <pre>
element to preserve whitespace,
<pre><c:out vaule="${myData}" /></pre>
或者只应用CSS 空格:pre
在父元素上,就像HTML < textarea>
元素在内部一样:
or just apply CSS white-space:pre
on the parent element, exactly like the HTML <textarea>
element is internally doing:
<span style="white-space:pre"><c:out value="${myData}"/></span>
(注意:a class
比风格
更受推荐,以上只是一个开创性的例子)
(note: a class
is more recommended than style
, the above is just a kickoff example)
后两种方法建议。 HTML代码不属于Java类。它属于JSP文件。更重要的是,您应该实际上使用JSTL < c:forEach>
来迭代集合,而不是整段Java代码。 b
The latter two approaches are recommended. HTML code does not belong in Java classes. It belongs in JSP files. Even more, you should probably actually be using JSTL <c:forEach>
to iterate over the collection instead of that whole piece of Java code.
<c:forEach items="${beanList}" var="bean">
<c:out value="${bean.name}" /><br />
</c:forEach>
这篇关于使用c出标签时不会出现新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!