带ManagedBeans的Primefaces始终返回null [英] Primefaces with managedBeans always return null

查看:82
本文介绍了带ManagedBeans的Primefaces始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我有一个表格 输入要通过发送到Bean,该对象已调试,在Bean对象中始终为null.您能帮我吗?

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.

代码在这里:

<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>

我使用SessionScoped和RequestScoped进行了测试,但无法正常工作,奇怪的是我已经做过其他类似的bean,并且它们是否可以工作 ManagedBean:

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();
    }

}

推荐答案

表单必须放在对话框中.

The form must go inside the dialog.

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

生成的对话框组件的HTML表示是在页面加载期间通过JavaScript重定位到<body>的末尾,以提高呈现模态对话框的跨浏览器兼容性.

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" />

删除该属性.它已经默认为@form,这正是您想要的.

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

这篇关于带ManagedBeans的Primefaces始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆