2013-03-14 103 views
0

有沒有辦法使用jstl直接修改css屬性? 下面是我想要做的使用jstl設置CSS樣式

<div id="error" align="center"> 
    <img src='resources/warning.png'/> 
    <c:if test="${not empty error}"> 
    <c:out value='${error}' />  //if i enter here, I want #error 'display' to be 'block' 
    </c:if> 
</div> 

所以你可能會說,爲什麼不只是有CSS設置爲display:block,並且放了,如果標籤這樣

<c:if test="${not empty error}"> 
    <div id="error" align="center"> 
     <img src='resources/warning.png'/>   
     <c:out value='${error}' />  
    </div> 
</c:if> 

這將被罰款,如果我的錯誤的唯一來源是來自控制器並傳回模型。但是,我也在做AJAX調用,並說瀏覽器端有問題,我仍然需要顯示錯誤。

因此,要做到這一點,例如在我的Ajax調用我做

$.ajax({ 

      url: "url", 
      beforeSend: function(){ 
       $("body").css("cursor", "wait"); 
      }, 
      dataType:"json", 
      timeout:15000, 
      cache: false, 
      async: false, 
      success: function(data){ 
       document.getElementById("error").innerHTML=""; 
       document.getElementById("error").style.display = "none"; 

         $("body").css("cursor", "default"); 
      }, 
      error: function(jqXHR, textStatus, errorThrown){ 
       if (textStatus =="timeout"){ 
        document.getElementById("error").innerHTML="Request timed out!"; 
       } 
       else{ 
        document.getElementById("error").innerHTML= jqXHR.responseText; 
       } 
      document.getElementById("error").style.display = "block"; 
     //  $("#loading").fadeOut('medium'); 
       $("body").css("cursor", "default"); 
      } 

     }); 
+0

爲什麼不給它一個CSS類名稱並使用樣式表爲您提供「顯示」值? – 2013-03-14 21:36:49

+0

我試圖保持它顯示:沒有,除非我的控制器返回一個'錯誤'。所以我的課程需要保持大部分相同,只是顯示器需要改變,如果錯誤存在 – 75inchpianist 2013-03-14 21:42:29

+0

將編輯帖子來解決我遇到的挑戰,不會允許某些解決方案 – 75inchpianist 2013-03-14 21:44:31

回答

0
<div id="error" align="center" <c:if test="${empty error}">style="display: none;"</c:if>> 
    <img src='resources/warning.png'/> 
    <c:if test="${not empty error}"> 
    <c:out value='${error}' /> 
    </c:if> 
</div> 

<div id="error" align="center" style="display: ${(empty error) ? 'none': 'block'};"> 
    <img src='resources/warning.png'/> 
    <c:if test="${not empty error}"> 
    <c:out value='${error}' /> 
    </c:if> 
</div> 

應該做你想要什麼。

+0

這實際上不工作,那麼它只會發現錯誤從控制器,不是JavaScript錯誤。我會發布我的解決方案 – 75inchpianist 2013-03-14 22:16:29

1

這裏是我是如何做到的

<div id="error" align="center"> 
    <img src='resources/critical.png'/> 
    <span id="errorText"> 
    <c:out value='${error}' /> 
    </span> 
</div> 

所以,現在的顯示屬性僅appying到包含div「錯誤」

我的JSP被設置在「ERRORTEXT」字段中的文本 我的AJAX腳本如果發現錯誤,也是這樣做的。

然後,在我的JavaScript,它會檢查是否「ERRORTEXT」中有什麼,並設置其顯示適當

$.ajax({ 
      //url: "./test.go?station="+stName+"&date="+rDate, 
      url: "./getAssignments.go?station="+stName+"&date="+rDate, 
      beforeSend: function(){ 
     // $("#loading").fadeIn('medium'); 
       $("body").css("cursor", "wait"); 
      }, 
      dataType:"json", 
      timeout:15000, 
      cache: false, 
      async: false, 
      success: function(data){ 

       document.getElementById("errorText").innerHTML=""; 
       document.getElementById("error").style.display = "none"; 
       pucksJSON=data; 
       reconstructGates(pucksJSON); 
       setStationName(stationName); 
       refresh_time = new Date(); //reset the time counter 
      // $("#loading").fadeOut('medium'); 
       $("body").css("cursor", "default"); 
      }, 
      error: function(jqXHR, textStatus, errorThrown){ 
       refresh_time = new Date(); //reset the time counter 
       if (textStatus =="timeout"){ 
        document.getElementById("errorText").innerHTML="Request timed out!"; 
       } 
       else{ 

        document.getElementById("errorText").innerHTML= jqXHR.responseText; 
        setStationName(stationName); 
        reconstructGates(null); 
       } 
      document.getElementById("error").style.display = "block"; 
     //  $("#loading").fadeOut('medium'); 
       $("body").css("cursor", "default"); 
      } 

     }); 

這隻能是因爲我建立了我的控制器返回,如果它不好的響應代碼設置錯誤。所以Ajax失敗並進入錯誤部分。