2016-07-13 25 views
3

我在.jsp文件的<head>中有此代碼。jsp上的條件語句不起作用

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

然後,我有這樣的:

<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在瀏覽器中:

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

我已經做這個嘗試控制檯,我得到正確的結果,但仍然沒有得到它的工作。

爲什麼會發生這種情況?

回答

3

問題是jsp執行完成後腳本標記中的東西在瀏覽器上執行。你不能使用pageId服務器端,它不存在於這種情況下。

嘗試

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

所以,這可能是解決方案嗎? – NietzscheProgrammer

+0

添加了一個示例來嘗試 – Taylor