JSTL, XPath and the State Tag Library
One of the projects I'm working on has a requirement to assign a user's permissions by state. I'm using the state tag library, which makes it very easy to make a drop-down of states. So, rather than creating a "states" table in the database, I wanted the ability to lookup state's abbreviations and full names in the tag library (since only abbreviations are stored in the database, as a comma-delimited list). So I used JSTL's XML tag to capture the output of this tag and then filter the states with XPath for the user's assigned states. Pretty slick IMO.
<label>Viewable States</label> <%-- Sets the available states so they can be parsed with XPath --%> <c:set var="statesHTML"> <state:state name="state"/> </c:set> <% // fake it for demo - real version has these as a bean property String[] states = {"CO", "AZ"}; pageContext.setAttribute("userStates", states); %> <x:parse var="states" xml="${statesHTML}"/> <%-- Loop through and parse the list of states --%> <x:forEach var="option" select="$states/select/option"> <c:forEach var="current" items="${userStates}" varStatus="status" > <x:if select="$option/@value[. = $current]"> <x:out select="$option/text()"/> <c:if test="${!status.last}">,</c:if> </x:if> </c:forEach> </x:forEach>