简体   繁体   English

表达式语言 (EL) 在 JSP 中的 if 条件中显示错误

[英]the Expression Language (EL) showing error in if condition in JSP

Im sending data from login.java to stu.jsp我将数据从 login.java 发送到 stu.jsp

below is the login.java code下面是 login.java 代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
       {
           boolean message = true;
           request.setAttribute("message", message); // This will be available as ${message}
           request.getRequestDispatcher("stu.jsp").forward(request, response);
       }

[this is the stu.jsp code (showing error)] Please click on the image to view the code with error 【这是stu.jsp的代码(显示错误)】请点击图片查看有错误的代码

click to view image点击查看图片

below is just the same code as above in the image which is showing an error in the if condition in EL下面是与上面图像中的代码相同的代码,它显示了 EL 中的 if 条件中的错误

<% if ( ${message} ) { %>
         <p> Today is weekend</p>
      <% } else { %>
         <p> Today is not weekend</p>
      <% } %>

Below is the complete error message when i run the server以下是我运行服务器时的完整错误消息

HTTP Status 500 – Internal Server Error


Type Exception Report

Message Unable to compile class for JSP: 

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [89] in the jsp file: [/stu.jsp]
Syntax error, insert ") Statement" to complete IfStatement
86:         
87:           
88:  
89:       <% if ( ${message} ) { %>
90:          <p> Today is weekend</p>
91:       <% } else { %>
92:          <p> Today is not weekend</p>


An error occurred at line: [89] in the jsp file: [/stu.jsp]
$ cannot be resolved to a variable
86:         
87:           
88:  
89:       <% if ( ${message} ) { %>
90:          <p> Today is weekend</p>
91:       <% } else { %>
92:          <p> Today is not weekend</p>


An error occurred at line: [89] in the jsp file: [/stu.jsp]
Syntax error on tokens, delete these tokens
86:         
87:           
88:  
89:       <% if ( ${message} ) { %>
90:          <p> Today is weekend</p>
91:       <% } else { %>
92:          <p> Today is not weekend</p>


An error occurred at line: [91] in the jsp file: [/stu.jsp]
Syntax error on token "else", delete this token
88:  
89:       <% if ( ${message} ) { %>
90:          <p> Today is weekend</p>
91:       <% } else { %>
92:          <p> Today is not weekend</p>
93:       <% } %>
94:     


An error occurred at line: [647] in the generated java file: [D:\Browser Downloads\setup -2\eclipse\TOMCAT\apache-tomcat-9.0.54\work\Catalina\localhost\PPP\org\apache\jsp\stu_jsp.java]
Syntax error, insert "}" to complete Block

Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:487)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    pack.login.doPost(login.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.

Please help me in resolving this issue Thank you in advance请帮我解决这个问题 提前谢谢

message is an attribute in JSP and everything inside <%... %> is java code. message是 JSP 中的一个属性, <%... %>都是 java 代码。

Inside Java code, you cannot use ${} as you can only use Java syntax.在 Java 代码中,您不能使用${}因为您只能使用 Java 语法。

Knowing this, you have two possibilities:知道了这一点,你有两种可能:

Either access message using request.getAttribute() inside <% %> :<% %>使用request.getAttribute()访问message

<% if ((Boolean)request.getAttribute("message")) { %>
         <p> Today is weekend</p>
      <% } else { %>
         <p> Today is not weekend</p>
      <% } %> 

Or you could use c:if :或者你可以使用c:if

<c:if test="${message}">
    <p> Today is weekend</p> 
</c:if>
<c:if test="${! message}">
    <p> Today is not weekend</p> 
</c:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM