我想用c:forEach打印Java列表,我有一个想法如何在jsp中打印列表。但是,我收到错误“未知标记c:forEach”。我一直在阅读,可能是由于依赖en Maven,所以我包含了依赖。之后,我看了,只有上述版本2.5允许使用此功能,所以我检查版本:使用JSTL打印Java列表的问题
Servlet version: 3.0
JSP version: 2.2
Java version: 1.8.0_25
我不知道要检查些什么,因为我继续有这个问题。下面我使用c:forEach包含了我的pom文件和源代码。也许别的东西在困扰。
的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库。阅读http://stackoverflow.com/tags/jstl/info –
我得到了错误“无法找到”http://java.sun.com/jsp/jstl/core“的标签库描述符,所以我改变了这: <%@ taglib uri =“http://java.sun.com/jsp/jstl/core”prefix =“c”%>,但也没有工作。我更新我的依赖在pom.xml中1.2,我改为1.2.1。 – Xalapa