jsp - 比较 c 中的 scriptlet 变量 :if

标签 jsp jstl

这是一些代码

<% 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 不能很好地协同工作。您应该选择其中之一。 scriptletofficially 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/

相关文章:

java - 使用 Bean 对象中的 JSON 构造数组

javascript - 是否可以从 jsp 的 java 部分执行 javascript 函数?

java - 如何在jsp中创建干净的url和MVC?

java - 非 jsf 应用程序的 ExternalContext 等价物

java - 在 Java 网站中实现注册的最佳方法

java - 在jsp页面中用java控制文本

java - Java Web 应用程序中的 JSTL 和 JSP 问题

java - 如何在JSP中格式化时间

tomcat - 马文 : cannot find the tag library descriptor

jSTL - 当未指定默认命名空间时,该函数必须与前缀一起使用