Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP global variable

I have this code

<%!  
   public String class_name = "";  
%>  
<c:choose>
      <c:when test="${WCParam.categoryId != null}">
        <% class_name = "my_account_custom"; %>
      </c:when>
      <c:otherwise>
        <% class_name = "my_account_custom_3"; %>
      </c:otherwise>    
</c:choose>    
<p>Class name = <c:out value='${class_name}' /></p>

WCParam.categoryId is null or not But my class_name variable is always empty. What i am doing wrong Thanks

like image 971
tinti Avatar asked Aug 30 '12 12:08

tinti


1 Answers

Scriptlets (<%...%>) and expression language (${...}) are completely different things, therefore their variables belong to different scopes (variables used in EL expressions are actually request attributes of different scopes).

If you decalred class_name as a scriptlet variable, you should access it using scriptlet as well:

<p>Class name = <c:out value='<%=class_name%>' /></p> 

However, you can write it without using a variable at all:

<p>Class name = <c:out 
     value='${WCParam.categoryId != null ? "my_account_custom" : "my_account_custom3"}' /></p>    
like image 80
axtavt Avatar answered Oct 07 '22 15:10

axtavt