jsp - JSP 中未计算 EL 表达式

标签 jsp el web.xml

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

<c:out value="${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);

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

最佳答案

Yes, i have doctype in web.xml <!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-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-21、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>

对于 Servlet 5.0(Tomcat 10、WildFly 22-26、GlassFish/Payara 6 等),如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns="https://jakarta.ee/xml/ns/jakartaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
    version="5.0">

    <!-- Config here. -->

</web-app>

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

当仍然有 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 中删除并集成到 JSP 2.0 中,JSP 2.0 与 Servlet 2.4 一起使用。所以,如果你的web.xml声明以 Servlet 2.3 或更旧的方式运行 webapp,那么 JSP 会期望在 JSTL 库中找到 EL,但如果它是较新的 JSTL 版本,缺少 EL,这又会失败。

另请参阅:

关于jsp - JSP 中未计算 EL 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59375696/

相关文章:

java - c 中的 JSTL 请求属性 :if

java - JSP/EL输出当前URL

eclipse - JSP EL(表达语言)在Eclipse中引起问题

html - 通过 EL 显示多行文本

linux - 如何在 hadoop 的 virtual box linux 中包含示例 helloworld jsp 文件?

java - Spring 3在jsp View 中绑定(bind)对象到表单

jsp - struts,包含jsp页面

servlets - @WebFilter 不同类型的不同初始化参数

jakarta-ee - Java EE - 搜索 web.xml 通用规范

java - 为什么在开始时尝试没有 "/"的 servlet 映射时会出现异常