0

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?

prabello
  • 556
  • 2
  • 14
  • 31
  • Just double-checking, you're saying that the first version of your code works fine, where you specify `#{shoppingCartBean.add(productDetailBean.id)}`? You get an integer in your method on submit? – DavidS Feb 23 '16 at 18:51
  • @DavidS yes, it works, but I would like to make the second version to work – prabello Feb 23 '16 at 18:52
  • Your question implies that it works when you use normal JSF components. Is this true? Please remove this implication otherwise. As to the technical problem, I guess your bean is badly scoped. This construct requires a view scoped bean. – BalusC Feb 24 '16 at 09:11
  • This is an example of using JSF as a regular MVC framework, for study purposes. Both beans are request scoped. – prabello Feb 24 '16 at 10:44

1 Answers1

0

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.

prabello
  • 556
  • 2
  • 14
  • 31