2017-08-13 627 views
1

我想顯示產品表中顯示的所有可用產品以顯示在此頁面上。我的jstl核心taglib被添加到頭文件中。jstl標籤<c:forEach不起作用

productList.jsp

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@include file="header.jsp"%> 

<div class="container-wrapper"> 
    <div class="container"> 
     <div class="page-header"> 
      <h1>All Products</h1> 

      <p class="lead">Checkout all the awesome products available now!</p> 
     </div> 

     <table class="table table-striped table-hover"> 
      <thead> 
      <tr class="bg-success"> 

       <th>Product Code</th> 
       <th>Product Name</th> 
       <th>Product Price</th> 
       <th></th> 
      </tr> 
      </thead> 
      <c:forEach items="${products}" var="product"> 
       <tr> 
        <td>${product.code}</td> 
        <td>${product.name}</td> 
        <td>${product.price}</td> 
        <td><a href="<spring:url 
        value="/product/viewProduct/${product.productId}" />" 
        > <span class="glyphicon glyphicon-info-sign"></span></a></td> 
       </tr> 
      </c:forEach> 
     </table> 
<jsp:include page="footer.jsp" /> 

我ProductController的是返回的產品清單。

ProductController的

@Controller 
public class ProductController { 
    @Autowired 
    ProductServiceImpl productService; 

    @RequestMapping(value="addProduct") 
    public String addProduct(){ 
     return ("addProduct"); 
    } 

    @RequestMapping(value = "/saveProduct") 
    public String saveProduct(@ModelAttribute("product") Product productInfo){ 
     productService.sace(productInfo); 
     return ("addProduct"); 
    } 
    @RequestMapping(value = "/productList", method = RequestMethod.GET) 
    public String productList(Model model){ 
     List<Product> productInfoList=productService.productList(); 
     model.addAttribute("products",productInfoList); 

     return "productList"; 
    } 
} 
當我運行在調試模式下,我可以清晰的看到產品的刀返回列表,所以沒有數據從數據庫中獲取的問題,這個代碼

ProductDao的

@Repository 
public class ProductDaoImpl implements ProductDao { 
    @Autowired 
    SessionFactory sessionFactory; 
    Session session; 
    Transaction tx; 

    public List<Product> productionList() { 
     session=sessionFactory.openSession(); 
     List<Product> productList=session.createQuery("from Product").list(); 
     return productList; 
    } 


} 

上的pom.xml依賴列表是

的pom.xml

<dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.8.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>4.3.8.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.3.8.RELEASE</version> 

    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.3.8.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>4.3.8.RELEASE</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>4.3.8.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.2.1.Final</version> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.36</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/taglibs/standard --> 
    <dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
     <scope>provided</scope> 
    </dependency> 
    </dependencies> 

當前產品列表頁

enter image description here

+0

請出示你的web.xml也。當servlet規範版本設置得太低時,這通常是一個問題。 – Gimby

回答

2

增加您.jsp頁面頂部的下方線並再次檢查

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

這已經添加到header.jsp文件中,並且header.jsp文件包含在productlist.jsp中 –

+0

請問您是否需要再次檢查? – sunkuet02

+0

我已多次檢查和驗證。即使我在productlist.jsp文件中添加了jstl核心taglib,它也不起作用。我很驚訝。 –

1

其實,這是非常難以回答的時候,我不知道你所有的代碼或程序的爲好。不過,我在過去使用jstl標籤也遇到了錯誤。只是試圖取代「的header.jsp」文件這一個:

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

你可以看到這裏的區別,我們增加了「core_rt」而不是「核心」。希望有所幫助!

0

添加下面一行<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" .....>

EL是封閉的初始化打擊版本web2.5