【发布时间】:2019-05-12 19:30:01
【问题描述】:
当我尝试使用 JSTL 时,JSP 无法识别我通过 servlet 提供的数据。我可以在 JSP 中使用 Java 代码访问请求属性,但不能使用 JSTL。
我正在使用 tomcat 服务器。我尝试在使用 maven 时更改 pom xml 中的一些依赖项。 现在我用的是1.2版本的jstl。
来自 pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
...
来自 servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data = "Some data, can be a String or a Javabean";
request.setAttribute("data", data);
request.setAttribute("userList", users);
RequestDispatcher rd = request.getRequestDispatcher("/DisplayUsers.jsp");
rd.forward(request, response);
}
...
来自位于 webapp 文件夹中的 .jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<head>
<title>Alle gespeicherten Nutzer</title>
<meta content = "text/html; charset = UTF-8"/>
</head>
<body>
<% String name = request.getAttribute("data").toString(); //this works
out.print(""+name);
%>
<p>The data from servlet: ${data}</p> // THIS DOESN´T recognize data
预期输出:来自 servlet 的数据:一些数据,可以是字符串或 Javabean 实际输出:来自 servlet 的数据:${data}
【问题讨论】: