【问题标题】:Primefaces with managedBeans always return null [duplicate]带有 managedBeans 的 Primefaces 总是返回 null [重复]
【发布时间】:2013-04-24 21:38:24
【问题描述】:

我有问题。我有一个表格 将输入发送到 bean 时,事物已被调试,在 bean 对象中始终为空。你能帮我解决这个问题吗?

代码如下:

<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,如果它们有效 托管豆:

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

}

【问题讨论】:

  • 请同时发布您的 presupuestoBean 代码
  • 你能发布你的堆栈跟踪吗?或者说哪个对象是空的
  • presupuesto 总是返回null,在jsf中只添加一个inputText,但它还有更多,这是presupuesto的属性。在将触发命令按钮的 actionListener 并调用方法 insertar 的那一刻,对象 presupuesto 为 null。
  • 尝试在 @postConstruct 中而不是在实际构造函数中启动 `presupuesto = new TbPresupuesto();`。
  • @nicknamecomment-reply-notifications 如果相关人员之前没有对帖子发表过评论,则不起作用 :) 只需以聪明的方式提出问题并耐心等待。

标签: jsf primefaces


【解决方案1】:

表单必须进入对话框。

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

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

使用您当前的代码,这意味着对话框将不再位于表单中。因此,当您尝试在对话框中提交输入值时,它们最终会变成空值,因为没有表单可以收集输入值并将其发送到服务器。

此外,您只是在命令按钮内处理提交按钮。

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

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

【讨论】:

  • 还是不行,我的解决办法是把输入的每一个id都放在流程里面,比如:process = "@this,year,month....." 谢谢@ BalusC
  • 完全删除process 属性。
  • 好的,谢谢,现在可以了!!!
猜你喜欢
  • 2011-02-17
  • 2019-08-05
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2021-07-24
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多