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>
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:
The function itself, which should be a 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.
A ".tld" file, if you don't already have one. It should end up in your webapp somewhere under "WEB-INF". Mine goes in a subdirectory called "tld", but you can put it anywhere.
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.
<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>
Related
I've got a string of html code with some additional tags inside. I need to get rid of the additional tags, but can't use fn:escapeXML because that would make the string no longer usable as html.
Example:
The value of "newLink" is set as:
test</span>">This is a <span class="help">test</span>
How can I use fn:replace (or some other jstl coding) to get rid of the inner tags?
This is what I've managed so far. Unfortunately, the last doesn't seem to match the empty tags.
<c:set var="displayValue">${fn:replace(pnxItem, 'span class=\"searchword\"', '')}</c:set>
<c:set var="displayValue">${fn:replace(displayValue, '/span', '')}</c:set>
<c:set var="displayValue">${fn:replace(displayValue, '><', '')}</c:set>
<c:set var="displayValue">${fn:replace(displayValue, '<>', '')}</c:set>
Wouldn't it be nice if you could use a regular expression? Jstl's replace function does not take a regex as an argument, as you probably already know, judging from the approach you have taken.
Luckily it is quite easy to write your own functions. I always carry along a class named StringFunctions, consisting of static functions, to be used as a custom functions library that holds, for instance, a removeTags function.
package com.whatever.viewhelpers;
public class StringFunctions {
public static String removeTags(String s) {
return s.replaceAll("\\<.*?>","");
}
// more functions ...
}
Now include this class in a tag library descriptor.
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.whatever.com/taglib/trlbt</uri>
<function>
<name>removeTags</name>
<function-class>
com.whatever.viewhelpers.StringFunctions
</function-class>
<function-signature>
String removeTags(java.lang.String)
</function-signature>
</function>
<!-- more functions -->
</taglib>
And use in jsp:
<%# taglib prefix="myfn" uri="/WEB-INF/taglib/tlb.tld" %>
....
${myfn:removeTags( ... )}
I chose myfn as prefix but you are free to choose whatever suits you.
Not sure if the proposed regex fully satisfies your needs, but it demonstrates the principle of custom functions. You can do anything Java allows you to in there, you could even use jsoup to get rid of the tags.
I am trying to render this string:
"<p>bold: <i>test</i> food <b>journal</b> entry</p>"
using jstl like these:
1) <c:out value="${topic.text}" escapeXml="true"/>
2) <c:out value="${topic.text}" escapeXml="false"/>
3) ${topic.text}
None of these work as expected. I want the text to be shown as html. However the results are (as printed on the browser):
1) <p>bold: <i>test</i> food <b>journal</b> entry</p>
2) bold: test food journal entry
3) bold: test food journal entry
How can I get:
test food journal entry
The last one you have ${topic.text} will output the raw string exactly as it is with no escaping, if that one isn't working the string is probably not what you think it is. Is there a chance that something is modifying the string such as the getText getter?
Use this.
<c:set var="str" value="<p>bold: <i>test</i> food <b>journal</b> entry</p>" />
<c:set var="str1">${str}</c:set>
${str1}
I have a problem that is just making me feel silly.... Given the following code in Razor:
#{
...
if (purchasedEvent.Address != null && purchasedEvent.Address != String.Empty){
addressBlock.AppendFormat("{0}<br />", purchasedEvent.Address);
}
...
}
#addressBlock.ToString()
The <br /> gets treated as literal text (that is to say I end up seeing something like 123 Cool Street<br />Anytown... rendered on the page. Changing the code (back) to addressBlock.AppendLine(purchasedEvent.Address) doesn't do any good either (renders 127 Cool Street Anytown.... What do I need to do to make the Razor engine respect that line break?
You need to use Html.Raw. To quote the docs: "Returns markup that is not HTML encoded."
So something like
#html.Raw(addressBlock.ToString())
The reason for it is that MVC is assuming that what you are giving it is the text as you want it to be seen and thus HTML encodes it. Raw allows you to tell it not to do that.
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,
sb.append("<br />");
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>
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>
(note: a class is more recommended than style, 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.
<c:forEach items="${beanList}" var="bean">
<c:out value="${bean.name}" /><br />
</c:forEach>
Here is my problem : in my database, I got a VACHAR2 of length 2000. Let's call it the "commentReception".
It is mapped to a String in my Struts bean.
The jspx (that handles both consult and update, within the form) looks that way :
<s:if test="%{consultAction}">
<s:textarea name="dto.commentReception" readonly="true" cssClass="textAreaValue readonly" />
</s:if>
<s:else>
<s:textarea name="dto.commentReception" readonly="false" cssClass="textAreaValue" />
</s:else>
When I submit the form, the bean is filled correctly, meaning without any \r\n at the end, and I get only one line, even in the database : no newlines.
But when I switch to consultAction, or let's say re-edit the form (not consultAction) another time, I get my string with an extra 2 newlines. That's really annoying and I don't know if it comes from Struts or from the tag. (with me probably missing a tag attribute?)
Of course, if I submit the form again, the comment string will be stored in the database with the newlines. So it will add two more new lines every saving of my comment...
Do you know where my problem comes from? Thanks already.
Edit : the generated form
<textarea class="textAreaValue" id="saveControleReception_dto_commentReceiption" rows="" cols="" name="dto.commentReceiption">commentaire contrôle avant reception.
</textarea>
You can see there are some newlines, but they are not taken into account in my bean. But they are somehow when I reload it.
So, it seems that the struts tag is adding by default two \r\n to the String upon loading the bean. That must be some kind of bug. So here is my solution :
<s:if test="%{consultAction}">
<textarea readonly="true" class="textAreaValue readonly"><s:property value="dto.commentReceiption"/></textarea>
</s:if>
<s:else>
<textarea class="textAreaValue" name="dto.commentReceiption"><s:property value="dto.commentReceiption"/></textarea>
</s:else>
I used the html tag instead of Strut's, and the property tag... If someone has a better answer (there must be), I'm still listening ;)
When you save or retreive your data add trim() in case you use mySQL. this will remove the remaining white spaces when you retrieve/seve it