Passing Page Values with Tiles and JSTL

Posted on

For added flexibility, where I work we’ve wanted to convert our non-application JSP page to PHP for some time now. In redesigning elements to be reusable, one of the things they incorporate is the ability to pass a value from a page to an included component, making the component generic and reusable and giving the page developer the ability to easily add custom elements like the names of script files or meta tag information.

Since we haven’t rolled that out yet and also wanted to use some of the same functionality in one of our applications, I worked out the following methods to pass that information to the page using JSPs. For specific elements, it’s easy enough to pass the information just using Java Tiles. In order to pass the parameters to the include, the Tiles include looks like this (note the pathing should be appropriate to the actual location of the included tile):

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:insertTemplate template="include/head.jsp" flush="true">
    <tiles:putAttribute name="title" value="This is my page"/>
    <tiles:putAttribute name="description" value="This is the page description"/>
    <tiles:putAttribute name="keywords" value="meta, tag, keyword, new keyword"/>
</tiles:insertTemplate>

And the values can be extracted in the include (head.jsp in this case):

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<title><tiles:getAsString name="title"/></title>
<meta name="description" content="<tiles:getAsString name="description"/>" />
<meta name="keywords" content="<tiles:getAsString name="keywords"/>" />

It’s a little more complex if you want to include a collection, such as a list of scripts to iterate through and include in a tile. In that case, you place the items in a list like so:

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<tiles:insertTemplate template="include/footer.jsp" flush="true">
  <tiles:putListAttribute name="scripts">
    <tiles:addAttribute type="string" value="js/specialscriptfile.js" />
    <tiles:addAttribute type="string" value="js/anotherspecialscriptfile.js" />
      <%-- additional script files would go here --%>
  </tiles:putListAttribute>
</tiles:insertTemplate>

The included script file names will be passed to the include by Tiles as a Java List that you can then iterate through. I prefer JSTL, so that’s the example here, though I imagine you could use a Struts iterate tag or whatever method you wished to avail yourself of (even a Java scriptlet,if you were so inclined, despite that being a bad practice).

<%@ taglib uri="/WEB-INF/tiles.tld" prefix="tiles" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- Scripts load here as a Yslow recommended best practice (for non-blocking requests) --%>
<tiles:insertTemplate template="scripts.jsp" flush="true" />
<%-- Scripts included from page using put-list-attribute --%>
<tiles:useAttribute id="list" name="scripts" classname="java.util.List" ignore="true"/>
<c:forEach var="item" items="${list}">
  <script type="text/javascript" src="<tiles:insertAttribute value="${item}" flush="true" />"></script>
</c:forEach><script item="" type="text/javascript" src="<tiles:insertAttribute value=">// <![CDATA[
">
// ]]></script>

Leave a Reply

Your email address will not be published. Required fields are marked *