2012-04-25 16 views
1

當我執行下面的代碼:錯誤:否則無if。但我有後,立即把它放在它

<script type="text/javascript"> 
function UploadMessage() { 
<%! String message = null; 
    boolean AttemptToUploadFile = false; 
%> 
    <% 
     message = (String)request.getAttribute("SuccessMessage"); 
     if(message != null) { 
    %> 
      alert("File Successfully Uploaded !"); 
      <% } %> 
      <% else if((Boolean)request.getAttribute("UploadAttempt")) { %> 
        alert("Unable to upload file"); 
       <%}%> 

} 

我收到以下錯誤:

media/work documents/UnderTest/NetbeansCurrent/ProjectSnippets/build/generated/src/org/apache/jsp/portfolio_005fone_jsp.java:252: error: 'else' without 'if' 
else if((Boolean)request.getAttribute("UploadAttempt")) { 
1 error 

錯誤說if沒有else,但我有在if之後立即放置else。那爲什麼這個錯誤?

回答

6

你必須把它們放在同一個腳本塊中。只是刪除結束%>並開始<%

<% } 
    else if (... 
+0

+1。簡單,準確,至關重要,完美的答案! – 2012-04-25 07:55:28

2

你的錯誤是在這兩行:

<% } %> 
<% else if((Boolean)request.getAttribute("UploadAttempt")) { %> 

在生成的Java代碼,他們將(基本上)被翻譯成:

} 
out.append("\n"); 
else if(... 

因此,您需要將左括號括在同一個腳本中,如下所示:

<% } else if((Boolean)request.getAttribute("UploadAttempt")) { %> 
0

您還沒有把它放在if之後。它之間有%> +換行符+ <%。 JSP必須在生成的代碼中輸出換行符,因此在生成的java代碼中存在諸如}else if之間的out.print("\n\t\t");(包括縮進)之類的內容。

相關問題