EL表达式未在JSP中评估

Gle*_*b S 13 jsp web.xml jstl el

我的servlets/jsp Web应用程序存在一个小问题.我正在尝试在jsp页面中使用jstl.当我使用任何标签时,例如:

<c:out value="${command}"/>
Run Code Online (Sandbox Code Playgroud)

它告诉我

${command} 
Run Code Online (Sandbox Code Playgroud)

在我的浏览器中而不是参数'command'值.我正在使用maven(我猜问题就在这里).这是pom xml依赖项:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我的web.xml声明标记:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
Run Code Online (Sandbox Code Playgroud)

和jsp部分:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
<title>Parsing results</title>
<link type="text/css" rel="stylesheet" href="css/page.css"/>
<link type="text/css" rel="stylesheet" href="css/table.css"/>
</head>

<body>
<h2 align="center">Results of 
parsing. Parsing method = <c:out value="${command}"/></h2>.......
Run Code Online (Sandbox Code Playgroud)

编辑:设置命令值的代码很简单:

request.setAttribute("command", parser.getName());
Run Code Online (Sandbox Code Playgroud)

然后去

request.getRequestDispatcher(redir).forward(request, response);
Run Code Online (Sandbox Code Playgroud)

请告诉我,我做错了什么!谢谢!

Bal*_*usC 30

是的,我在web.xml中有doctype <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "java.sun.com/dtd/web-app_2_3.dtd"; >

删除<!DOCTYPE>web.xml所有应该很好.一个有效的Servlet 3.0(Tomcat 7,JBoss AS 6/7,GlassFish 3等)兼容<web-app>如下所示,没有任何web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <!-- Config here. -->

</web-app>
Run Code Online (Sandbox Code Playgroud)

对于Servlet 3.1(Tomcat 8,WildFly 8/9/10/11,GlassFish/Payara 4等),它如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- Config here. -->

</web-app>
Run Code Online (Sandbox Code Playgroud)

对于Servlet 4.0(Tomcat 9,WildFly 12,GlassFish/Payara 5等),它如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
    version="4.0">

    <!-- Config here. -->

</web-app>
Run Code Online (Sandbox Code Playgroud)

当使用JSTL 1.1或更新版本时,您需要确保<!DOCTYPE>以webapp以至少Servlet 2.4模式运行的方式声明您的声明,否则EL表达式将无法在webapp中运行.

当仍然有一个Servlet 2.3或更早版本web.xml<!DOCTYPE>,即使你已经有一个Servlet 2.4或更新的XSD,它仍然会被强制在Servlet 2.3或更旧的modus中运行,导致EL表达式失败.

技术原因是,EL最初是JSTL 1.0的一部分,在Servlet 2.3/JSP 1.2及更早版本中不可用.在JSTL 1.1中,EL从JSTL中删除并集成在JSP 2.0中,后者与Servlet 2.4一起使用.因此,如果<web-app>声明您在Servlet 2.3或更旧版本中运行webapp,那么JSP会期望在JSTL库中找到EL,但如果它是较新的JSTL版本,缺少EL,则会失败.

也可以看看: