这是一些代码
<% String what = (String)session.getAttribute("BANG"); %>
<c:set var="bang" value="Song" />
我从 session 中获取一个字符串,我想将它与 jSTL 变量中的字符串进行比较。
我尝试过 if
<% if() { %> some text <% } %>
也尝试过
<c:if test="${va1 == va2}" </c:if>
最佳答案
对于初学者,建议停止使用 scriptlet,那些 <% %>
事物。它们与 JSTL/EL 不能很好地协同工作。您应该选择其中之一。 scriptlet 是 officially discouraged十年以来,停止使用它们是有道理的。
回到你的具体问题,以下脚本
<% String what = (String)session.getAttribute("BANG"); %>
可以在 EL 中按如下方式完成
${BANG}
所以,这应该适合你:
<c:if test="${BANG == 'Song'}">
This block will be executed when the session attribute with the name "BANG"
has the value "Song".
</c:if>
或者,如果您确实需要 "Song"
在一个变量中,那么:
<c:set var="bang" value="Song" />
<c:if test="${BANG == bang}">
This block will be executed when the session attribute with the name "BANG"
has the same value as the page attribute with the name "bang".
</c:if>
另请参阅:
关于jsp - 比较 c 中的 scriptlet 变量 :if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695581/