0

I want to print all my products in a table on a JavaServer page but I have troubles with my jstl code.

My Model product:

public final class Product
{
    private int id;
    private String description;    
    private Double price;    
    private String categoryName;
...

My JavaBean products: (when this bean is created it's filled with products from my model)

@ManagedBean(name = "productsBean")
@RequestScoped
public class Products implements Serializable {

    private List<Product> producten;
    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;

    public Producten() {

        Store store = applicationBean.getStore();

    for (String c : store.getCategories()) {
        for(model.Product p : store.getProductsOfCategory(c)){
            beans.Product product = new Product();
            product.setId(p.getId());
            product.setDescription(p.getDescription());
            product.setCategoryName(p.getCategoryName());
            product.setPrice(p.getPrice());
        producten.add(product);
        }

    }                
    }

My JavaBean Product:

@ManagedBean(name= "productBean")
@RequestScoped
public class Product implements Serializable{

    @ManagedProperty(value = "#{applicationBean}")
    private ApplicationBean applicationBean;

     private int product_id;
    private String description;    
    private Double price;    
    private String categoryName;

In my JavaServer Page I want something like that but:

            <c:forEach var="product" items="${productsBean.products}">
                <tr>
                    <td>${product.description}</td>
                </tr>
            </c:forEach>
user1939400
  • 51
  • 2
  • 9

2 Answers2

0

The legacy JSP expression ${} does not auto create JSF managed beans if they do not exist in the scope yet. You need the JSF expression #{} instead. Even more, you should stop using the legacy JSP expression ${} in JSF pages to avoid future confusion and maintenance trouble. The JSF expression #{} does exactly the same and more (namely, auto creating JSF managed beans and setting model values via input components).

<table>
    <c:forEach var="product" items="#{productsBean.products}">
        <tr>
            <td>#{product.description}</td>
        </tr>
    </c:forEach>
</table>

See also:


Unrelated to the concrete problem, have you considered using <h:dataTable> instead? It saves you from fiddling with loose HTML <table>, <tbody>, <tr>, <th> and <td> tags.

<h:dataTable value="#{productsBean.products}" var="product">
    <h:column>#{product.description}</h:column>
</h:dataTable>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I use now, but my webpage shows literally this "#{product.description}". What am I doing wrong? – user1939400 Jan 04 '13 at 09:33
  • you might want to check the servlet specification version number in your web.xml. – Gimby Jan 04 '13 at 10:48
  • That may happen if you're using JSP instead of Facelets, or if your classpath is a complete mess, or if JSF 2 runs in JSF 1.x fallback modus due to bad configuration. Which resources/tutorials/books are you using while learning JSF2? Based on your previously asked questions I don't have the impression that it's a sane one. – BalusC Jan 04 '13 at 11:28
-1

hi did you import the TLD files of jsf jsf core and html

<f:view>
<h:form>
<h:dataTable value="#{productsBean.products}" var="product">
<h:column>#{product.description}</h:column>  
</h:dataTable>

try like this Thanks

Ayaz Ali Khatri
  • 483
  • 4
  • 13