0

This is my first app using JSF so I'm a little confused. I need to read a value between Managed beans but it is always null no matter how many solutions I try. Recently I noticed that my @PostConstruct method is being called multiple times causing the value to be lost. I already looked at other posts with this issue but haven't been able to solve it.

I'm using JSF 2.2

I appreciate any kind of help, thanks!

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.tiendavirtual.model.Cliente;
import com.tiendavirtual.model.Compra;
import com.tiendavirtual.model.Item;
import com.tiendavirtual.model.Producto;
import com.tiendavirtual.service.ICompraServiceLocal;
import com.tiendavirtual.service.IProductoServiceLocal;

@ViewScoped
@ManagedBean
public class CompraBean {

@EJB
private ICompraServiceLocal compraService;

@EJB
private IProductoServiceLocal productoService;

private Cliente clienteCarrito;


@PostConstruct
public void init() {
    productoActual = new Producto();
    itemActual = new Item();
    carritoCompra = new ArrayList<Item>();
    precioTotal = 0;
    cantidadProductosCarrito = 0;
    getClienteActual();
}

 ... setters and getters
codenoob
  • 257
  • 3
  • 15
  • What is the concrete JSF implementation version? Is it invoked on every post back (Ajaxical or otherwise)? – Tiny Jul 06 '15 at 21:21
  • *"I need to read a value between Managed beans"*. What exactly do you mean here? Are you navigating from one view to another view and you expected two different views to reference the same view scoped bean instance in some way? – BalusC Jul 06 '15 at 21:23
  • I'm using JSF 2.2 mojarra. For now I just noticed that it is called twice when the dialog that uses this Managed bean is shown. – codenoob Jul 06 '15 at 21:24
  • Hi @BalusC, I have a datatable in one view which has client data. I need to pass the client that was selected in this table to the managed bean which code is posted. I have tried many solutions that I found but none appears to work because the Postconstruct of this method is being called twice – codenoob Jul 06 '15 at 21:54
  • You're still not clear on to the technical and the code posted so far does not represent [a real MCVE](http://stackoverflow.com/tags/jsf/info), so it's hard to post an answer. So, here's just a link which I think you're actually looking for: http://stackoverflow.com/q/8459903 – BalusC Jul 06 '15 at 22:00
  • PostConstruct will only be called twice if it _should_ be called twice; that is, if you issue a new GET request or start POSTing to a new page, you will get a new ViewScoped bean, as per the spec. – DavidS Jul 06 '15 at 22:05

0 Answers0