JSF pass through elements can't send objects

171 views Asked by At

I'm using JSF pass through element, and i want to send to my Bean an entire object, but when i do i get null.

I'm doing something like this:

<f:metadata>
<f:viewParam    id="id" name="id" value="#{productDetailBean.id}"/>
<f:viewAction   action="#{productDetailBean.loadBook()}"/>              

<form jsf:id="form" method="post" class="container">
            <ul id="variants" class="clearfix">
                <li class="buy-option"><input type="radio" name="id"
                    class="variant-radio"
                    id="product-variant-#{productDetailBean.book.id}"
                    value="#{productDetailBean.book.id}" checked="checked" /> 
                    <label class="variant-label"
                    for="product-variant-#{productDetailBean.book.id}"> E-book
                        + Impresso </label>
                    <small class="compare-at-price">R$ #{productDetailBean.book.price}</small>
                    <p class="variant-price">R$ #{productDetailBean.book.price}</p>
                </li>
            </ul>

            <button type="submit" jsf:action="#{shoppingCartBean.add(productDetailBean.id)}" class="submit-image icon-basket-alt"
                title="#{productDetailBean.book.title}">Comprar</button>

And my Bean:

public String add(Integer id){
    Book book = bookDao.search(id);
    ShoppingItem shoppingItem = new ShoppingItem(book);
    shoppingCart.add(shoppingItem);
    return "/site/carrinho?faces-redirect=true";
}

I would like to do the following:

<button type="submit" jsf:action="#{shoppingCartBean.add(productDetailBean.book)}" class="submit-image icon-basket-alt"
                title="#{productDetailBean.book.title}">Comprar</button>

And the bean:

public String add(Book book){
    ShoppingItem shoppingItem = new ShoppingItem(book);
    shoppingCart.add(shoppingItem);
    return "/site/carrinho?faces-redirect=true";
}

But when i do it, i get a null book, is it possible to do it?

1

There are 1 answers

3
prabello On

The code works because of the <f:viewParam id="id" name="id" value="#{productDetailBean.id}"

Unlike the book, the id is being set on the JSF component tree, so when you execute the code the id is filled, but the book is not.