我在 <head>
中有这段代码的 .jsp
文件。
<script type="text/javascript">
var pageId = "<c:out value="${pageData.contentId}" />";
</script>
然后我有这个:
<p>
<strong>
Some text here
<br>
<c:if test="${pageId == 'how-credit-affects-interest-rate' || pageId == 'taking-control-credit-score'}">
Display text here if conditional is true.
</c:if>
</strong>
</p>
我已经尝试这样做了:
<c:if test="${pageId eq 'how-credit-affects-interest-rate' || pageId eq 'taking-control-credit-score'}">
Display text here if conditional is true.
</c:if>
还尝试了 or
而不是 ||
,如果我把 pageId
在浏览器控制台中,我得到了正确的结果,但仍然无法正常工作。
为什么会发生这种情况?
最佳答案
问题是脚本标签中的内容在jsp执行完成后在浏览器上执行。您不能在服务器端使用 pageId
,它在该上下文中不存在。
尝试
<c:if test="${pageData.contentId eq 'how-credit-affects-interest-rate' || pageData.contentId eq 'taking-control-credit-score'}">
关于java - jsp 上的条件语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38358160/