小编典典

JSP中未评估EL表达式

jsp

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

<c:out value="${command}"/>

它告诉我

${command}

在我的浏览器中,而不是参数“ 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>

我的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">

和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>.......

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

request.setAttribute("command", parser.getName());

然后去

request.getRequestDispatcher(redir).forward(request, response);

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


阅读 358

收藏
2020-06-08

共1个答案

小编典典

是的,我在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并确保<web-app>已声明声明符合Servlet
2.4或更高版本,并且一切都应该很好。有效的Servlet 3.0兼容(Tomcat 7,JBoss AS 6/7,GlassFish 3等)
的整体*web.xml外观如下所示,没有任何内容: *<!DOCTYPE>

<?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>

对于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>

对于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>

使用JSTL 1.1或更高版本时,您需要确保web.xml以这样的方式声明您的身份:web应用程序至少以Servlet
2.4方式运行,否则EL表达式将无法在webapp中运行。

当仍然有一个Servlet 2.3或以上<!DOCTYPE><web-app>web.xml,即使你已经有一个Servlet
2.4或更新版本的XSD,那么它仍然会被迫在Servlet的2.3或以上作案运行,使EL表达式失败。

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

2020-06-08