Alireza Fattahi
Alireza Fattahi

Reputation: 45555

c:set is not working in JSTL 1.2

I am making a dynamic menu base on an xml. This XML will be parsed and build the final menu. The xml is:

<menus>
    <menu>
        <name>menu.level1.Home</name>
        <action>transfer-to-account-input</action>
    </menu>
    <menu>
        <name>menu.level1.Accounts</name>
        <action>accounts-summary</action>
    </menu>
</menus>

I used jstl to parse the XML. the x:set seems not working!

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%>

<x:forEach select="$docRoot/menus/menu" var="menuvar">
    <x:set select="$menuvar/name" var="menuName1" >
    <c:set var="menuName2">
        <x:out select="$menuvar/name" />
    </c:set>
    ${menuName1} //This doesn't show any thing
    ${menuName2} //This works!
</x:forEach>

Am I making a mistake or this is a bug in JSTL 1.2

Upvotes: 0

Views: 695

Answers (1)

Paul Sweatte
Paul Sweatte

Reputation: 24627

The syntax of x:set is referencing $menuVar using the XPath variable syntax, but menuVar is a JSTL variable. It should be:

<x:set select="$pageScope:menuvar/name" var="menuName1" >

Using JSTL Data as XPath Variables Scoped variables can be used in XPath expressions ($implicitObject:variableName) similar to how they are used in EL (${implicitObject.variableName}). If the implicit object is omitted, scopes will be searched in standard order. Note that the “.” and “[]” notations cannot be used for accessing bean properties.

References

Upvotes: 1

Related Questions