@postConstruct method with parameter

1.6k views Asked by At

I've got a list of orders on a database, and I want to show two separate datatables in two JSF pages: one table regarding all orders, and one table regarding the current logged user. Problem is, only the first one is actually showed on the page.

JSF page with the datatable links

<h:commandLink action="#{ordineController.listaOrdini}"
        value="Consulta gli ordini esistenti" rendered="#{not empty loginAdmin.admin.email}"/>
    <div>
        <h:commandLink
            action="#{ordineController.listaOrdiniCliente}"
            value="Controlla i tuoi ordini"
            rendered="#{not empty loginCliente.clienteLoggato.email}">
            <f:setPropertyActionListener target="#{ordineController.clienteCorrente}"
                value="#{loginCliente.clienteLoggato}" />
        </h:commandLink>
</div>

ViewScoped bean

     @ManagedBean(name="ordineController")
@ViewScoped
public class OrdineController implements Serializable {

/**
     * 
     */
    private static final long serialVersionUID = 1L;
    @PostConstruct
    public void init() {
        ordini = oFacade.getListaOrdini();
        listaOrdiniCliente();
    } 
    public String listaOrdini() {
        this.ordini = oFacade.getListaOrdini();
        return "showOrdini"; 
    }

    public String listaOrdiniCliente() {
        this.ordiniCliente = oFacade.getOrdiniCliente(clienteCorrente);
        return "showOrdiniCliente"; 
    }
/*Getters and setters*

The JSF page that doesn't work (showOrdiniCliente.xhtml)

<h:outputText value="Non c'è nessun ordine."
        rendered="#{empty ordineController.ordiniCliente}" />
    <h:dataTable id="lista" value="#{ordineController.ordiniCliente}"
        var="ordine" rendered="#{not empty ordineController.ordiniCliente}">

The JSF page that does work (showOrdini.xhtml)

<h:outputText value="Non c'è nessun ordine."
    rendered="#{empty ordineController.ordini}" />
<h:form rendered="#{not empty ordineController.ordini}">
    <h:dataTable id="lista" value="#{ordineController.ordini}"
        var="ordine">

Why is the #{ordineController.ordiniCliente} empty? Shouldn't it be builded along with the "ordini" variable in the @PostConstruct?

edit Facade method (it retrieves all the orders of a customer)

public List<Ordine> getOrdiniCliente (Cliente cliente) {

    try {
        TypedQuery<Ordine> q = em.createQuery("SELECT ord FROM Ordine ord WHERE ord.cliente = :cliente", Ordine.class);
        q.setParameter("cliente", cliente);
        return q.getResultList();

    } 
    catch (Exception e) {
        String q = "Il cliente " +cliente.getNickname()+ " non ha creato degli ordini";
        System.out.println(q);
        return null;

    }

}
0

There are 0 answers