JSP全局变量

tin*_*nti 4 java jsp global-variables

我有这个代码

<%!  
   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>
Run Code Online (Sandbox Code Playgroud)

WCParam.categoryId为null或不是但我的class_name变量始终为空.我做错了什么谢谢

axt*_*avt 5

Scriptlets(<%...%>)和表达式语言(${...})是完全不同的东西,因此它们的变量属于不同的范围(EL表达式中使用的变量实际上是不同范围的请求属性).

如果您class_name作为scriptlet变量进行decalred ,您也应该使用scriptlet访问它:

<p>Class name = <c:out value='<%=class_name%>' /></p> 
Run Code Online (Sandbox Code Playgroud)

但是,您可以在不使用变量的情况下编写它:

<p>Class name = <c:out 
     value='${WCParam.categoryId != null ? "my_account_custom" : "my_account_custom3"}' /></p>    
Run Code Online (Sandbox Code Playgroud)