13

Is possible to make output generated by my own JSP tags to be shorter ? For example tag defined as below generate 5 lines instead of 1. Is possible to avoid that (without join all 5 lines into 1 in tag source) ?

<%@ tag description="link" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="href" required="true" type="java.lang.String" %>
<%@ attribute name="label" required="false" type="java.lang.String" %>
<a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>

not a solution:

<%@ tag description="standard input" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ attribute name="href" required="true" type="java.lang.String" description="address relative to web-app context" %><%@ attribute name="label" required="false" type="java.lang.String" description="link label" %><a href="<c:url value="${href}"/>">${not empty label ? label : href}</a>
marioosh
  • 27,328
  • 49
  • 143
  • 192
  • This is not an exact dupe of the other question: how does one trim the whitespace output by a custom tag without simply removing all whitespace within the tag? You cannot use <%@ page trimDirectiveWhitespaces="true" %> within a tag. – werkshy Jan 08 '13 at 21:48
  • 3
    The correct answer is that one can add this in the tag: `<%@ tag trimDirectiveWhitespaces="true" %>` – werkshy Jan 08 '13 at 21:52
  • should be reopened because the correct answer (see comments) is different from the original question's. – Sean Patrick Floyd Jan 09 '13 at 20:39

3 Answers3

18

As already pointed out by werkshy, to avoid whitespace being generated by directives used in a JSP custom tag,

<%@ tag trimDirectiveWhitespaces="true" %>

can be used (<%@ page trimDirectiveWhitespaces="true" %> does not help in this case, as it only seems to apply to directives in the JSP itself and not in the custom tags used by the page).

However, to allow this tag attribute, JSP version 2.1 might need to be specified e.g. using an implicit.tld (as described on https://docs.oracle.com/javaee/5/tutorial/doc/bnamu.html or https://forums.oracle.com/thread/742224) which then needs to be placed into directory with the tags. (At least I needed to do that for WebLogic 12c.)

implicit.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
    <tlib-version>1.0</tlib-version>
    <short-name>implicit</short-name>
</taglib>
anre
  • 3,617
  • 26
  • 33
7

Yes, you can globally configure the JSP parser to trim whitespace which are left by script expressions and tags.

Add this to your webapp's web.xml (which has to be Servlet 2.5 compatible!):

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
</jsp-config>

If you target a Servlet 2.4 container or lower, then you have to edit container's own web.xml instead to apply this globally. In Tomcat for example, it's the /conf/web.xml file. Search for the <servlet> declaration of the JspServlet and add the following servlet init parameter inside the <servlet> declaration.

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You should bold **Servlet 2.5 compatible** – Vlad Aug 10 '11 at 13:15
  • 1
    @Vlad: Well, 5 years ago I would do. But nowadays, Servlet 2.5 is already out for 5 years... I'd expect everyone to be using that already ;) That exclamation mark is sufficient I think. – BalusC Aug 10 '11 at 13:16
  • 2
    `` works only for .jsp pages (the same as `<%@ page trimDirectiveWhitespaces="true" %>`), but not work for my own tag generated output. Only `trimSpaces` as init param works at all and get 1-line output of my tag. – marioosh Aug 11 '11 at 08:37
3

In your JSP:

<%@ page trimDirectiveWhitespaces="true" %>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I'm using Tomcat 7.0.19 (Servlet 3.0, JSP 2.2) and `<%@ page trimDirectiveWhitespaces="true" %>` works on jsp page, but not on tag source (`link.tag` in my example). Using that directive on page where my own tag is used doesn't reduce output of tag. – marioosh Aug 11 '11 at 08:24
  • @marioosh ok, well then maybe you should just trim() your tag's output – Sean Patrick Floyd Aug 11 '11 at 08:26
  • How to do that with tag like in my question ? – marioosh Aug 11 '11 at 08:33
  • @marioosh sorry I thought you were using a custom tag – Sean Patrick Floyd Aug 11 '11 at 08:45
  • 3
    This works for me in Tomcat 6: `<%@ tag trimDirectiveWhitespaces="true" %>` I wish the question could be re-opened to add a real answer. – werkshy Jan 08 '13 at 21:52
  • @werkshy add your answer to the original (linked) question then: http://stackoverflow.com/questions/208736/strip-whitespace-from-jsp-output – Sean Patrick Floyd Jan 09 '13 at 08:45
  • Linked question is specifically about a page, this question is specifically about a tag. It seems off-topic to add this to the other question. In my case I was trying to use the output of a custom tag as a url e.g. ` – werkshy Jan 09 '13 at 20:31