java - jSTL 标签 <c :forEach is not working

标签 java spring

我想显示来自产品表的所有可用产品以显示在此页面上。我的 jSTL 核心 taglib 添加到头文件中。

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 正在返回产品列表。

产品 Controller

@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";
    }
}

当我在 Debug模式下运行这段代码时,我可以清楚地看到 dao 返回的产品列表,因此从数据库中获取数据没有问题。

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

最佳答案

在您的.jsp 页面顶部添加以下行并再次检查

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

关于java - jSTL 标签 <c :forEach is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45659651/

相关文章:

java - Spring mvc 不调用 Controller

java - java浮点变量初始化

java - JPA/Hibernate 级联删除同时删除 ManyToOne 关系

java - 我们如何使用 mongoTemplate 为 Mongodb Collection 实现分页

java - 如何在 Java 中匹配括号内的字符串(嵌套)?

java - 如何找到 Jersey 使用的反序列化器

java - Autowiring 到列表中时的 Bean 顺序

spring - 如何在 SpEL 中按类型引用 bean?

java - Spring Cloud : How to use Feign without Ribbon

java - 将 Spring MVC 的 Controller 与 HTTPServlet 解耦