aj_us
aj_us

Reputation: 119

<c:set and <c:if in spring

I am using c:set and c:if in jsp (i am using Spring MVC 3)

<c:set var="myVar" value="${JavaClass.MY_KEY}"/>  
<c:if test="${myVar == code}">  
    Do Somthing  
</c:if>  

but in this case c:if code is not getting executed.

When i give (without c:set)

<c:if test="${'ABC' == code}">  
    Do Somthing  
</c:if>     

it workes.

Can anyone tell me what i am doing wrong

Thanks

Upvotes: 3

Views: 24696

Answers (2)

Steven Benitez
Steven Benitez

Reputation: 11055

You can't access JavaClass.MY_KEY directly using JSP EL, but you can do it with a scriptlet one-liner*.

<c:set var="myVar" value="<%=JavaClass.MY_KEY%>"/>

* In general, scriptlets should be avoided, but these types of one-liners which can reference static fields and enum constants are useful until JSP EL offers a better way of accessing them.

Upvotes: 4

JB Nizet
JB Nizet

Reputation: 691755

${JavaClass.MY_KEY} looks for an object in page, then request, then session, then application scope, stored under the JavaClass attribute name. If it finds one, it calls the method getMY_KEY() on this object.

I suspect this is not what you thought this expression was doing. Since there is no such object in any scope, it evaluates to the empty string (or null, I couldn't say for sure).

There is no way to access class constants using JSP EL. The best you can do is to store some object in some scope, with getters returning the constants.

Upvotes: 4

Related Questions