在 jSTL 中使用来自 scriptlet 的变量的正确方法是什么? 我不知道我的代码有什么问题:
<%
boolean a = true;
boolean b = false;
%>
<c:choose>
<c:when test="${a}">
<c:set var="x" value="It's true"/>
</c:when>
<c:when test="${b}">
<c:set var="x" value="It's false"/>
</c:when>
</c:choose>
看起来它并没有进入整个街区。
最佳答案
Scriptlet 中的变量在 JSTL 中是看不到的,因为表达式语言,${}
之间的东西在 JSTL 中使用,将在页面、请求、 session 或应用程序中查找属性。您至少必须将来自 scriptlet 的变量存储在这些范围之一中,然后使用它。
这是一个例子:
<%
boolean a = true;
request.setAttribute("a", a);
%>
<c:if test="${a}">
<c:out value="a was found and it's true." />
</c:if>
更多信息:
作为建议,停止使用 scriptlet。将 JSP 中的业务逻辑移动到 Controller ,将 View 逻辑移动到 EL、JSTL 和其他标签,如 <display>
.更多信息:How to avoid Java code in JSP files?
关于java - 如何将 java 变量从 scriptlet 传递到 c :when expression in jSTL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25893913/