home.


Tagged: jsp


JSP: Create a tag file

Put this in your WEB-INF/tags folder. It’s called box.tag:

    <%@ attribute name="colour" required="true" rtexprvalue="true" %>
    <div style="background-color: ${colour}">
       <jsp:doBody />
    </div>

Note the attribute directive. It’s saying the tag requires a parameter called ‘color’ and we can add in run-time expressions if we won’t (i.e. not static text only).

You can also pass a ‘type’ to the attribute tag. This is for when you’re passing an object, not a String. It must be the fully qualified class name.

You can pass dynamic attributes, too. This means any attribute you specify on the calling tag will end up in a list of dynamic attributes that you can iterate over in your tag file:

    <%@ tag dynamic-attributes="dynattrs" %>
    ...
    <c:forEach var="a" items="${dynattrs}">
       ${a.key}="${a.value}"
    </c:forEach>
    ...

Now you’re specifying, in the ‘tag’ directive, that all the dynamic attributes will be in a property called ${dynattrs}.

Now reference this from your JSP file:

    <%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %> 
    <mytag:box colour="#ccc" something="dynamic">
       Hiya
    </mytag:box>

Note the taglib directive has the taglib attribute that points to the directory where our box.tab file is. We give this a prefix.

Then we call a xml like tag, with the name above as the namespace, followed by the name of the tab. We add in the required attribute ‘color’. And the content between the tags.

We could also use this more verbose version:

    <%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %> 
    <mytag:box>
       <jsp:attribute name="color">#ccc</jsp:attribute>
       <jsp:body>hi ho</jsp:body>
    </mytag:box>
jsp jsp-tag-files

JSP: If else in JSTL

And if statement in JSTL is:

    <c:if test="${true}">
       Hiya
    </c>

And if-else is actually choose-when-otherwise:

    <c:choose>
       <c:when test="${true}">
         Hiya
       </c:when>
       <c:otherwise>
         Bye
       </c:otherwise>
    </c:choose>

Don’t forget to include the jstl taglib.

jsp jstl jstl-if jstl-when

JSP: Creating a custom EL function

Note I’m creating this custom EL tag in Tomcat 7.

First make a class with a static method like so:

    package your.package;

    public class Functions {

       public static String hello(String name) {
         return "Hiya, " + name + ".";
       }

    }

Then make a file called mytaglib.tld in WEB-INF/tags/:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <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 http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" 
      version="2.1"> 

      <tlib-version>1.0</tlib-version>
      <uri>http://www.your.url/tablib_name</uri>

      <function>
          <name>hello</name>
          <function-class>your.package.Functions</function-class>
          <function-signature>java.lang.String hello(java.lang.String)</function-signature>
      </function>  

    </taglib> 

Note the function part, which specified the name of the new EL function, what class it resides in and its function signature.

The uri would be used if we were accessing this directly, but instead we’ll be accessing the url from the web.xml below:

    <web-app 
        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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">

        ...

       <jsp-config>
       <taglib>
         <taglib-uri>
           http://some.thing/mine
         </taglib-uri>
         <taglib-location>
           /WEB-INF/tags/mytaglib.tld
         </taglib-location>
       </taglib> 
       </jsp-config>

       ...

     </web-app>

Note we’re pointing to the mytaglib.tld file just created. And taglib-uri is how we’ll refer to it in the JSP:

    <%@ taglib uri="http://some.thing/mine" prefix="a" %> 

    ${a:hello("Aaron")}
jsp jsp-el el-custom-function

JSP: Setup JSTL and EL in Tomcat 7

If you want to use JSTL (and you do) instead of scriptlets you first have to include it in your war file or your tomcat lib file.

I imported it into my project using Gradle. You should see the jar file in the created WAR file once you’re using it.

Now you can use things like:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
    <c:forEach var="p" items="${applicationScope['products'].values().iterator()}">

       ${p.name} | ${p.getPrice()} || <a href="Cart?add=true&id=${p.getId()}">add</a>
       <br />

    </c:forEach>

Note you don’t need the scriptlet %@ page import.. directives anymore. You do need thee taglib directive to point to the jstl core library.

Note we’re using the JSP EL, expression language, syntax to call method and attribute (p.name is the same as p.getName()). EL comes with Tomcat 7 - no additional libraries are required.

As with scriplets there are implicit objects in EL, applicationScope, in this example. ’applicationScope[‘products’] is the same as applicationScope.getAttribute(‘products’).

jsp jstl jstl-foreach tomcat jsp-el

JSP: Changing the page encoding to UTF-8

Put this in your web.xml to change the page encoding:

    <jsp-config>
       <jsp-property-group>
         <url-pattern>*.jsp</url-pattern>
         <page-encoding>UTF-8</page-encoding>
       </jsp-property-group>
    </jsp-config> 

Now any jsp file you render, from a servlet or wherever, will have the UTF-8 charset in its response headers, i.e:

    Content-Type:text/html;charset=UTF-8
jsp jsp-charset

Page 1 of 2
Next