Navigating complex data structures using JSTL

This is a simple example showing how to navigate through a complex data structure of inner objects using JSTL.

InnerJSTLTest.jsp looks like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
			<title>Inner JSTL Test</title>
	</head>
	<body>
		<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
		<table width="100%" class="table">
			<tr>
				<td>
					<b>JSTL Iteration</b>
				</td>
			</tr>

			<tr>
				<td>
					<table width="100%" class="table" cellpadding="0"
						cellspacing="5" border="0" bgcolor="#FFFFFF">
						<c:set var="listsize" value="1" scope="page" />
						<c:forEach var="arrayElement" items="${messageVO.outerArrayList}"
							varStatus="listsize">
							<c:set var="currentlistindex" value="${listsize.count}"
								scope="page" />
							<br />
							<tr>
								<td width="20%">
									<b>
										<u>
											Section
											<c:out value="${currentlistindex}" />
										</u>
									</b>
									<br />
									<c:forEach var="outerMapElement" items="${arrayElement}">
										<b>Sub-Section: ${outerMapElement.key}</b>
										<br />
										<c:set var="flag" value="0" scope="page" />
										<c:forEach var="innerMapElement" items="${outerMapElement.value}"
											varStatus="innermapsize">
											<u>Sub-Sub-Section: ${innerMapElement.key}</u>
											<br />
											<c:forEach var="innerArrayElement" items="${innerMapElement.value}">
												<c:forEach var="elem" items="${innerArrayElement}">
													<li>${elem}</li>
													<br />
												</c:forEach>
											</c:forEach>
											<c:set var="flag" value="${innermapsize.count}" scope="page" />
										</c:forEach>
										<c:if test='${flag== 0}'>
											<p style="color:#009900">
												<b>EMPTY SUB SECTION!</b>
											</p>
										</c:if>
									</c:forEach>
									<hr />
								</td>
							</tr>
						</c:forEach>

					</table>
				</td>
			</tr>
		</table>
	</body>
</html>


JSTLTestServlet.java looks like this:


package com.mattiz.jstlTest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JSTLTestServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse res) {
		try {
			JSTLTestVO jstlTestVO = new JSTLTestVO();
			req.setAttribute("messageVO", jstlTestVO);
			getServletConfig().getServletContext()
					.getRequestDispatcher("/InnerJSTLTest.jsp")
					.forward(req, res);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		doPost(req, res);
	}
}

JSTLVO.java which builds up the data structure and makes it available to the jsp looks like this


package com.mattiz.jstlTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSTLTestVO {

	private List outerArrayList;

	public List getOuterArrayList() {
		return populateVO();
	}

	public void setOuterArrayList(List outerArrayList) {
		this.outerArrayList = outerArrayList;
	}

	public List populateVO() {
		List innerList1 = new ArrayList();
		innerList1.add("Java Rocks");
		innerList1.add("Down With M$");
		innerList1.add("Ever Tried Java?");
		innerList1.add(".NET sucks");
		innerList1.add("Make Java while the SUN shines");
		innerList1.add("Java has Everything Under The SUN!");
		innerList1.add("God Bless the SUN");
		innerList1.add("Java rules!");
		List innerList2 = new ArrayList();
		innerList2.add("IBM= I Blame Microsoft!");
		innerList2.add("MAC = Most Applications Crash!");
		innerList2.add("MAC= Most Applications Crash Or System Hangs!");
		List innerList3 = new ArrayList();
		innerList3.add("Good Day!");
		innerList3.add("Guten Tag!");
		innerList3.add("Au Revoir!");
		List innerList4 = new ArrayList();
		innerList4.add("Damn It!");
		innerList4.add("It sucks!");
		Map innermap1 = new HashMap();
		innermap1.put("Java", innerList1);
		Map innermap2 = new HashMap();
		innermap2.put("Acronyms", innerList2);
		innermap2.put("Greetings", innerList3);
		Map innermap3 = new HashMap();
		Map outerMap1 = new HashMap();
		outerMap1.put("Geek Tongue", innermap1);
		outerMap1.put("Mother Tongues", innermap2);
		Map outerMap2 = new HashMap();
		outerMap2.put("Empty?", innermap3);
		List outerArrayList = new ArrayList();
		outerArrayList.add(outerMap1);
		outerArrayList.add(outerMap2);
		return outerArrayList;
	}
}

web.xml looks like this for starters:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	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">
	<display-name>InnerJSTL</display-name>
	<servlet>
		<servlet-name>JSTLTestServlet</servlet-name>
		<servlet-class>com.mattiz.jstlTest.JSTLTestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>JSTLTestServlet</servlet-name>
		<url-pattern>/JSTLTestServlet</url-pattern>
	</servlet-mapping>
</web-app>

The URL to run the app is

http://localhost:8080/JSTLInner/JSTLTestServlet

The output looks something like this:

JSTL Iteration
Section  1
Sub-Section:  Mother Tongues 
Sub-Sub-Section:  Greetings
Good Day!
Guten Tag!
Au Revoir!
Sub-Sub-Section:  Acronyms
IBM= I Blame Microsoft!
MAC = Most Applications Crash!
MAC= Most Applications Crash Or System Hangs!
Sub-Section:  Geek Tongue
Sub-Sub-Section:  Java
Java Rocks
Down With M$
Ever Tried Java?
.NET sucks
Make Java while the SUN shines
Java has Everything Under The SUN!
God Bless the SUN
Java rules!
--------------------------------------------------------------------------------
Section  2
Sub-Section:  Empty?
EMPTY SUB SECTION