sagar limbu
sagar limbu

Reputation: 1262

jstl tag <c:forEach is not working

i want to show all the available products coming from product table to be displayed on this page. my jstl core taglib is added on header file.

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" />

my productController is returning list of products.

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

when i run this code in debug mode i can clearly see dao returning list of product so there is no problem of data fetching from database.

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


}

the list of dependencies on pom.xml are

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>

the current productlist page is

enter image description here

Upvotes: 1

Views: 4803

Answers (3)

Erich
Erich

Reputation: 91

add the below line <%@ page isELIgnored="false"%>
after <%@ taglib prefix="c" .....>

EL is closed initialized blow the version web2.5

Upvotes: 3

Nam Bui
Nam Bui

Reputation: 11

Actually, it is quite hard to answer when I do not know all of your codes or process as well. However, I also got an error in the past with jstl tags. Just try to replace this one in "header.jsp" file:

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

You can see the difference here, we added "core_rt" instead of "core". Hope that helps!

Upvotes: 1

sunkuet02
sunkuet02

Reputation: 2442

Add the below line on the top of your .jsp page and check again

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

Upvotes: 4

Related Questions