org.apache.jasper.JasperException: #{…} is not allowed in template text [duplicate]

时间秒杀一切 提交于 2019-12-02 08:33:43

问题


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



来源:https://stackoverflow.com/questions/18548556/org-apache-jasper-jasperexception-is-not-allowed-in-template-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!