简体   繁体   中英

Conditional statement on jsp isn't working

I have this code in the <head> of the .jsp file.

<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 || , if I put pageId in the browser console, I get the proper result but still I don't get it working.

Why this could be happening?

The problem is the stuff in the script tag executes on the browser after the jsp execution has finished. You can't use pageId server side, it doesn't exist in that context.

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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