tags:

views:

564

answers:

4

is there a good way to trim whitespaces generated by JSPs without resorting to the following techniques:

  • trimSpaces jasper directive: may ruin your layout by eating significant spaces where you want them
  • surrounding areas you want to trim with JSP comments, commenting out the spaces. this makes the JSP harder to edit and looks horrible

Id like to know if anyone knows a better way to selectively trim spaces in JSPs

edit: what i do now is put all my tags on one like like this:

<c:forEach var="date" items="${model.list}"><%--
--%><c:set var="dateStr"><ct:dateFormat date="${date.startDate}"/></c:set><%--
--%><option value="${dateStr}">${dateStr}</option><%--
--%></c:forEach>
+1  A: 

You're dealing with a least-of-all-evils choice here. I would go with the jasper trimSpaces directive and if you have whitespace that is significant in your layout, you can strategically insert whitespace like this:

<c:out value=" " />

or like this:

${ }

Other options for trimming whitespace that haven't been mentioned in your post are:

  • Write a servlet filter that trims whitespace as a post render step (This will have to be clever enough to not trim your "significant" whitespace).
  • Put all your jsp tags on one line (just kidding, don't do this!)
Asaph
maybe if i was starting a new web app that would be a choice, but to go back over 40K lines of code and insert spaces.. id need to outsource that.
mkoryak
@mkoryak: I added some more strategies. See my updated answer.
Asaph
i do end up putting all my tags on one line, ive updated my question with an example.
mkoryak
A: 

eating spaces where you want them

Can you give an example? I really can't imagine of such a need.

Do you mean inside textareas? Inside HTML <pre> elements? Inside CSS white-space:pre styled elements? The Tomcat's trimSpaces setting should nicely take them into account.

Or do you mean spaces for layout? Well, they really doesn't belong there. Consider replacing by CSS margin/padding properties and keep the trimSpaces setting.

BalusC
An example of "eating spaces where you want them": `<c:out value="${person.firstname}" /> <c:out value="${person.lastname}" />`. With jasper's `trimSpaces`, the firstname and lastname get squished together with no whitespace in between. Of course there is an elegant workaround: `<c:out value="${person.firstname} ${person.lastname}" />`.
Asaph
Ah yes, I see. Thanks.
BalusC
exactly what Asaph said. Also the elegant workaround doesnt always work that easily if your JSP has lots of conditionals, loops etc, then its harder to combine output like that without using lots of setters etc.
mkoryak
those spaces can effectively be   /  
Bozho
A: 

check out Trim filter from JSOS: http://www.servletsuite.com/servlets/trimflt.htm

Den
A: 

Try: http://coldjava.hypermart.net/servlets/trimflt.htm Just implemented it myself as servlet filter, works a charm.

Another option is: htmlcompressor (google it, I can only provide 1 link atm...)

The taglib of htmlcopressor allows you to use a wrapper, also has compressors for css and js.

BGerrissen