Reputation: 8720
How can I display my list in a TestArea line after line with no additional spaces. i.e:
this
that
the
other
Here is my attempt:
<div class="text">
<label for="output_string">Output:</label> `
<textarea rows="10" cols="20">
<c:forEach var="x" items="${messagelist}">${x}</c:forEach>
</textarea>
</div>
Upvotes: 2
Views: 5046
Reputation: 15
<c:set var="xv"></c:set>
<c:forEach items="${messagelist}" var="x">
<c:if test="${not empty x}">
<c:choose>
<c:when test="${idx.first}"><c:set var="xv" value="${x}"></c:set></c:when>
<c:otherwise><c:set var="xv" value="${xv},${x}"></c:set></c:otherwise>
</c:choose>
</c:if>
</c:forEach>
<textarea cols="45" rows="5">${xv}</textarea>
Upvotes: 0
Reputation: 413737
Here's a guess (which I'll try out in just a sec in one of my own pages):
<c:forEach var='x' items='${messagelist}'><c:out value='${x}\r\n'/></c:forEach>
edit — no that doesn't seem to work at all. However, what did work was for me to add a message catalog entry like this:
linebreak={0}\r\n
Then you can use <fmt:message key="linebreak"><fmt:param value="${x}"/></fmt:message>
to produce the string terminated by line breaks.
Note that JSP will put spaces before the first entry according to the indentation in your .jsp source file before the <c:forEach>
, so you'll have to line everything up at the left edge if you don't want that.
If I had to do this a lot, I'd write an EL add-on function of my own to echo back a string followed by CRLF.
edit — If you want to write an EL add-on, you need two things:
public static
method of some class. I keep a class around called "ELFunctions" for most of mine. You can arrange them any way you want.So you would write a little function like this, in some class:
public static String linebreak(final String msg) {
return msg + "\r\n";
}
Then your ".tld" file would look like this (assuming it's the only thing you've got; if you have an existing ".tld" file just add the clause):
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
<description>Your Favorite Description</description>
<display-name>Make Something Up</display-name>
<tlib-version>4.0</tlib-version>
<short-name>whatever</short-name>
<uri>http://yourdomain.com/tld/whatever</uri>
<function>
<description>
Return a string augmented with trailing line break (CR - LF pair)
</description>
<name>linebreak</name>
<function-class>your.package.name.YourClass</function-class>
<function-signature>
java.lang.String linebreak(java.lang.String)
</function-signature>
</function>
(Boy, XML is so annoying.) Now somewhere you probably already have a little file that pulls in taglibs for your pages (for <c:...>
tags at least). In there, or at the top of any page, add a line like this:
<%@ taglib prefix="whatever" uri='http://yourdomain.com/tld/tango' %>
I think that the JSP runtime searches for ".tld" files by looking through the WEB-INF subtree, and in .jar files in WEB-INF/lib, matching by that "uri" string. Anyway, once you've done that, in your JSP file you can say:
<c:forEach var='x' items='${messagelist}'>${whatever:linebreak(x)}</c:forEach>
and it'll invoke your function.
Upvotes: 2