【问题标题】:error : else without if . But i have placed it immediately after it错误:否则没有 if 。但我已经把它放在它之后
【发布时间】:2012-04-25 07:04:44
【问题描述】:

当我执行以下代码时:

<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。那为什么会出错呢?

【问题讨论】:

    标签: java javascript jsp


    【解决方案1】:

    您必须将它们放在同一个脚本块中。只需删除结尾的%&gt; 和开头的&lt;%

    <% }
         else if (...
    

    【讨论】:

      【解决方案2】:

      你的错误在这两行:

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

      在生成的 java 代码中,它们将(基本上)被翻译成:

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

      因此,您需要将右括号放在同一个 scriptlet 中,如下所示:

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

      【讨论】:

        【解决方案3】:

        你没有把它放在if 之后。它之间有%&gt; + 换行+ &lt;%。 JSP 必须在生成的代码中输出换行符,因此在生成的 java 代码中 }else if 之间会有类似 out.print("\n\t\t"); 的内容(包括缩进)。

        【讨论】:

          猜你喜欢
          • 2018-08-11
          • 1970-01-01
          • 1970-01-01
          • 2016-01-28
          • 1970-01-01
          • 2011-07-18
          • 1970-01-01
          • 1970-01-01
          • 2019-02-20
          相关资源
          最近更新 更多