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 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.Apart from using the HTML
<br>element instead of the linefeed character,and printing it without
<c:out>like so${myData}, you can also use the HTML<pre>element to preserve whitespace,or just apply CSS
white-space:preon the parent element, exactly like the HTML<textarea>element is internally doing:(note: a
classis more recommended thanstyle, the above is just a kickoff example)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.