简体   繁体   English

关于jsp的条件语句不起作用

[英]Conditional statement on jsp isn't working

I have this code in the <head> of the .jsp file. 我在.jsp文件的<head>中有这段代码。

<script type="text/javascript">
    var pageId = "<c:out value="${pageData.contentId}" />";
</script>

And then I have this: 然后我有这个:

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

I already tried by doing this: 我已经尝试过这样做:

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

And also tried with or instead of || 并尝试使用or代替|| , if I put pageId in the browser console, I get the proper result but still I don't get it working. ,如果我把pageId放在浏览器控制台中,我得到了正确的结果但仍然没有让它工作。

Why this could be happening? 为什么会发生这种情况?

The problem is the stuff in the script tag executes on the browser after the jsp execution has finished. 问题是在jsp执行完成后,脚本标记中的内容在浏览器上执行。 You can't use pageId server side, it doesn't exist in that context. 您不能使用pageId服务器端,它在该上下文中不存在。

Try 尝试

<c:if test="${pageData.contentId eq 'how-credit-affects-interest-rate' || pageData.contentId eq 'taking-control-credit-score'}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM