java JSP 全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12196617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP global variable
提问by tinti
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
WCParam.categoryId 是否为空但我的 class_name 变量始终为空。我做错了什么谢谢
回答by axtavt
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).
Scriptlets( <%...%>
)和表达式语言( ${...}
)是完全不同的东西,因此它们的变量属于不同的作用域(EL表达式中使用的变量实际上是不同作用域的请求属性)。
If you decalred class_name
as a scriptlet variable, you should access it using scriptlet as well:
如果你被标记class_name
为一个 scriptlet 变量,你也应该使用 scriptlet 访问它:
<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>