Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shorten output generated by custom JSP tag?

Tags:

java

jsp

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>
like image 500
marioosh Avatar asked Aug 10 '11 13:08

marioosh


2 Answers

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>
like image 114
anre Avatar answered Sep 17 '22 23:09

anre


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>
like image 25
BalusC Avatar answered Sep 19 '22 23:09

BalusC