有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误:没有if的else。但我把它放在了后面

当我执行以下代码时:

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


共 (3) 个答案

  1. # 1 楼答案

    你必须把它们放在同一个脚本块中。只需删除结尾%>和开头<%

    <% }
         else if (...
    
  2. # 2 楼答案

    您没有立即将其置于if之后。它之间有%>+newline+<%。JSP必须在生成的代码中输出换行符,因此在生成的java代码中}else if之间有类似out.print("\n\t\t");(包括缩进)的内容

  3. # 3 楼答案

    您的错误在以下两行:

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

    在生成的java代码中,它们(基本上)将被转换为:

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

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

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