2015-07-03 48 views
1

我想用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的问题。

非常感谢您的帮助。

干杯

+1

不要对当前策略和策略列表使用相同的变量名称。并且在JSP的顶部用'<%@ taglib prefix =“c”uri =“http://java.sun.com/jsp/jstl/core”%>'指令声明JSTL库。阅读http://stackoverflow.com/tags/jstl/info –

+0

我得到了错误“无法找到”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

回答

2

包括与下一行JSP中taglibe,它是要解决你的问题:

<%@ taglib prefix="c" 
      uri="http://java.sun.com/jsp/jstl/core" %> 

检查下一个链接以获取更多信息http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

- - 编辑-----

使用此依赖关系

<dependency> 
    <groupId>jstl</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.2</version> 
</dependency> 

,而不是这个

<dependency> 
    <groupId>javax.servlet.jsp.jstl</groupId> 
    <artifactId>jstl-api</artifactId> 
    <version>1.2</version> 
</dependency> 
+0

我不知道为什么我在taglib中发生错误。无法找到“http://java.sun.com/jsp/jstl/core”的标记库描述符,因此我将其更改为 <%@ taglib uri =“java.sun.com/jsp/jstl/core” ;前缀=“c”%>,但我得到了同样的错误:( – Xalapa

+0

请检查我的编辑请 –

+0

添加依赖和重建和刷新项目 – underdog

1

在JSP你需要包括JSTL标记

<%@ 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"> 
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Current Records:</title> 

而且在你C:的forEach用户不同的变量名迭代

  <c:forEach items="${policies}" var="policy"> 
      <tr> 
       <td><c:out value="${policy.id}" /></td> 
       <td><c:out value="${policy.max_books}" /></td> 
       <td><c:out value="${policy.year_book}" /></td> 
       <td><c:out value="${policy.activate}" /></td> 
      </tr> 
     </c:forEach> 

也根据你的评论尝试添加maven depe你的pom中的状态

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.2</version> 
</dependency>