springmvc 不解析EL表达式的解决办法

来源:互联网 发布:gymnopedie no.1 知乎 编辑:程序博客网 时间:2024/05/26 02:51

这是一个spring mvc初学者可能会遇到的问题。代码如下:

@Controller@RequestMapping(value="/home")public class HomeController {    @RequestMapping(value = "/index")    public ModelAndView Index(Model model)    {        model.addAttribute("msg", "hello,springmvc");        return new ModelAndView("index");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<html><head><meta http-equiv="Content-Type" content="text/html;"><title>Insert title here</title></head><body>${msg}</body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行结果不是我们希望的hello,springmvc,而是${msg}。 
导致这个结果的原因是JSP的版本问题,有可能你的项目目前的默认版本是1.2,这个版本默认是不开启EL表达式的。

解决办法有两个,最直接的办法是,在JSP页面的上方加入一个标签,开启EL表达式

<%@ page isELIgnored="false" %>
  • 1

当然,如果每个页面都要加这么一行的话也很不爽,你可以将你的web.xml中的web-app节点加上下面的属性

 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"      version="3.1">      ......  </web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

就会使用JSP2.0,而JSP2.0默认是开启EL表达式的。


我在写这篇博客是也很纠结,解决问了以后,本来想重现一下错误,把代码改回来,改回来后却发现EL表达式也可以解析了。不得已,为了搞明白到底是为什么,我又新建了一个项目,把主要的代码拷贝过来,问题又重现了。我改完web.xml文件,并没有马上生效,而是等了几秒种后我又刷新页面才生效的。我又把web.xml中的代码撤销,重新部署,EL表达式也神奇般地解析了

阅读全文
0 0