比较c:if中的scriptlet变量

时间:2012-12-04 03:23:58

标签: jsp jstl

这是一些代码

<% String what = (String)session.getAttribute("BANG"); %>
<c:set var="bang" value="Song" />

我从会话中得到一个字符串,我想将它与jstl变量中的字符串进行比较。

我在

尝试过if
<% if() { %> some text <% } %> 

也尝试了

<c:if test="${va1 == va2}" </c:if>

1 个答案:

答案 0 :(得分:2)

对于初学者,建议停止使用 scriptlet ,那些<% %>的东西。它们与JSTL / EL不能很好地协同工作。你应该选择其中一个。由于 scriptlets 是十年以来的officially discouraged,因此停止使用它们是有意义的。


回到您的具体问题,以下 scriptlet

<% 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>

另见:

相关问题