1

I have a problem. I have a form with inputs to be sent to a bean by , the thing is debugged, in the bean object is always null. Could you help me with this.

Here the code:

<h:form id="frmNuevo">
      <p:dialog id="dialog" header="Añadir Presupuesto" widgetVar="dialogNuevo" resizable="true" width="500" height="500" showEffect="fade" hideEffect="explode" modal="true">
        <p:growl id="growl" showDetail="true" sticky="true" />
        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin: 0 auto;">
            <h:outputText value="Diciembre:" />
            <p:inputText value="#{presupuestoBean.presupuesto.diciembre}" required="true"                 maxlength="20" />
            <p:commandButton value="Guardar" update=":form:cars, frmNuevo, growl, display" process="@this" actionListener="#{presupuestoBean.insertar}" oncomplete="dialogNuevo.hide()" image="icon-save" />
            <p:commandButton value="Cancelar" update=":form:cars" oncomplete="dialogNuevo.hide()" style="margin-bottom: 20px;" image="icon-cancel" />
        </h:panelGrid>
        <p:separator/></p:dialog>
</h:form>

I tested with SessionScoped and RequestScoped and does not work, the strange thing is I have done other similar beans and if they work ManagedBean:

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "presupuestoBean")
@RequestScoped
public class PresupuestoBean implements Serializable {

    private TbPresupuesto presupuesto;
    private List<TbPresupuesto> presupuestos;
    private UploadedFile file;
    private String destination = "C:\\temp\\";

    public PresupuestoBean() {
        presupuesto = new TbPresupuesto();
        presupuestos = new ArrayList();
    }

    public TbPresupuesto getPresupuesto() {
        return presupuesto;
    }

    public void setPresupuesto(TbPresupuesto presupuesto) {
        this.presupuesto = presupuesto;
    }


    public void prepararInsertar() {
        presupuesto = new TbPresupuesto();
        presupuestos = new ArrayList();
    }

    public void insertar() {
        PresupuestoDaoImpl presupuestoDao = new PresupuestoDaoImpl();
        presupuesto.setPresupuestoId(presupuesto.getLinea().getLineaId());
        presupuestoDao.insertar(presupuesto);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Se ha ingresado correctamente"));
        presupuesto = new TbPresupuesto();
    }

}
SRy
  • 2,743
  • 7
  • 33
  • 54
Yaser Nicveg
  • 23
  • 1
  • 4
  • please also post your presupuestoBean code – Andres L Apr 24 '13 at 21:57
  • can you please post your stacktrace? or say which object is null – Andres L Apr 24 '13 at 22:19
  • presupuesto always return null, in jsf just add one inputText, but it has more, which are attributes of presupuesto. In the moment that will fired the actionListener of the command button and call to the method insertar, the object presupuesto is null. – Yaser Nicveg Apr 24 '13 at 22:24
  • try to initiate ` presupuesto = new TbPresupuesto();` inside a `@postConstruct` rather than in Actual constructor. – SRy Apr 24 '13 at 22:28
  • hi @SrinivasR, this annotation is of javax.annotation.PostConstruct or library JSF??? – Yaser Nicveg Apr 24 '13 at 22:48
  • i test with this and no work: @PostConstruct public void init(){ presupuesto = new TbPresupuesto(); } – Yaser Nicveg Apr 24 '13 at 22:55
  • Hi @BalusC can you help me with this? – Yaser Nicveg Apr 24 '13 at 23:28
  • 1
    `@nickname` comment-reply-notifications doesn't work if the person in question hasn't commented on the post before :) Just ask the question the smart way and have a bit of patience. – BalusC Apr 25 '13 at 00:49

1 Answers1

4

The form must go inside the dialog.

<p:dialog>
    <h:form>
        ...
    </h:form>
</p:dialog>

The generated HTML representation of the dialog component is namely during page load by JavaScript relocated to the end of <body> in order to improve cross browser compatibility of presenting a modal dialog.

With your current code, this thus means that the dialog will then not sit in a form anymore. So when you try to submit the input values inside the dialog, they will end up as nulls because there is no form in order to collect and send the input values to the server.

Further, you're only processing the submit button inside the command button.

<p:commandButton ... process="@this" />

Remove that attribute. It defaults to @form already, which is exactly what you want.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452