【发布时间】:2015-07-03 17:11:04
【问题描述】:
我正在尝试使用 c:forEach 打印 Java 列表,我知道如何在 jsp 中打印列表。但是,我收到错误“Unknown tag c:forEach”。我一直在阅读这可能是由于 Maven 的依赖关系,所以我包含了依赖关系。后来,我读到只有2.5以上的版本才允许使用这个功能,所以我检查了版本:
Servlet version: 3.0
JSP version: 2.2
Java version: 1.8.0_25
我不知道还要检查什么,因为我仍然遇到此问题。下面我包含了我的 pom 文件和使用 c:forEach 的源代码。可能还有其他问题。
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RESTful</groupId>
<artifactId>clientLibrary</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>clientLibrary</name>
<build>
<finalName>clientLibrary</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Servlet (deletePolicy.java):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
Client client= Client.create();
WebResource webResource= client.resource("http://localhost:8080/clientLibrary/webapi/policy");
//create an object of RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("GetPolicy");
// send the client data available with req of delete to req of getPolicy with include()
rd.include(request, response);
List<Policy> policies = (List<Policy>) request.getAttribute("policies");
printWriter.print("List of books in Delete: ");
for(Policy policy : policies) {
printWriter.println("<li>"+"ID: "+policy.getId()+"<br>"+"Max Number of Books: "+policy.getMax_books()+"<br>"+"Year of Book: "+policy.getYear_book()+"<br>"+"Activated: "+policy.getActivate()+"<br></li><br>");
}
//Show to the user the possible options to delete using radio button
request.setAttribute("policies", policies);
RequestDispatcher rd2 = getServletConfig().getServletContext().getRequestDispatcher("/showRecordsToDelete.jsp");
rd2.include(request,response);
//Receive the answer
printWriter.print("I am comming back from showRecordsToDelete.jsp");
/*ClientResponse rs=webResource.accept(
MediaType.APPLICATION_JSON_TYPE,
MediaType.APPLICATION_XML_TYPE).
delete(ClientResponse.class,input);
printWriter.print("Delete a policy");*/
}
showRecordsToDelete.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Current Records:</title>
</head>
<body>
<h2>Select a policy to be deleted:</h2>
<table>
<%-- JSTL foreach to loop a list retrieve from a servlet(DeletePolicy.java) in jsp --%>
<c:forEach items="${policies}" var="policies">
<tr>
<td><c:out value="${policies.id}" /></td>
<td><c:out value="${policies.max_books}" /></td>
<td><c:out value="${policies.year_book}" /></td>
<td><c:out value="${policies.activate}" /></td>
</tr>
</c:forEach>
</table>
<p>I will show the records</p>
</body>
</html>
我的想法是显示记录列表并使用单选按钮选择记录并将信息返回给 servlet。但是,在尝试打印列表时,我遇到了未知标签 c:forEach 的问题。
提前感谢您的帮助。
干杯
【问题讨论】:
-
不要对当前策略和策略列表使用相同的变量名。并在 JSP 的顶部使用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>指令声明 JSTL 库。阅读stackoverflow.com/tags/jstl/info -
我收到错误“找不到“java.sun.com/jsp/jstl/core”的标签库描述符,所以我改为:java.sun.com/jsp/jstl/core" prefix="c"% > ,但也没有用。我在 pom.xml 中更新了我的依赖项而不是 1.2 ,我改为 1.2.1。
标签: java jsp maven servlets foreach