20030906 Saturday September 06, 2003

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>

Posted in Java at Sep 06 2003, 11:43:57 AM MDT Add a Comment

Comments:

Post a Comment:
  • HTML Syntax: Allowed
Click me to subscribe
Matt Raible is a Web Architecture Consultant specializing in open source frameworks.
« May 2012
SunMonTueWedThuFriSat
  
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
  
       
Today

Recent Entries

Tag Cloud