org.apache.jasper.JasperException: #{…} is not all

2019-03-01 05:32发布

问题:

This question already has an answer here:

  • #{…} is not allowed in template text 3 answers

I was trying to display a simple message on browser screen with JSP and Spring MVC.

<h2>#{message}</h2>

However, it threw the below exception:

org.apache.jasper.JasperException: /Ekle/DomainEkle.jsp (line: 9, column: 6) #{...} is not allowed in template text
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:103)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:733)
org.apache.jasper.compiler.Node$ELExpression.accept(Node.java:954)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1798)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)

How is this caused and how can I solve it?

回答1:

Use

  <h2>${message}</h2>

Instead of

  <h2>#{message}</h2>

The ${...} is the expression language used in JSP, check more here

The #{...} is the expression language related to JSF technology, check more here



回答2:

To display/print a ModelAttribute in JSP, you have to use the ${..} notation. So in your case you should use

<h2>${message}</h2>

Instead, if you want to access an object field, you should use the dot notation.

Example:

public Person {
     private String name;
     private String surname;

     public Person(String name, String surname) {
          this.name = name;
          this.surname = surname;
     }

     //getter and setter goes here

}

Controller class:

@Controller
public ExampleController {
    @RequestMapping("/test")
    public ModelAndView testObject() {

         Person p = new Person("Steven","Hawking");
         return new ModelAndView("test", "person", p);
    }
}

in page

<h2>${person.name} ${person.surname}</h2>

will display

Steven Hawking