我有问题。我有一张表格 将输入发送到bean,调试东西,在bean对象中始终为null。你能帮帮我吗?
这里是代码:
<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:
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();
}
}
答案 0 :(得分:4)
表格必须进入对话框。
<p:dialog>
<h:form>
...
</h:form>
</p:dialog>
生成的对话框组件的HTML表示即在页面加载期间,JavaScript重定位到<body>
的末尾,以便提高呈现模式对话框的跨浏览器兼容性。
使用您当前的代码,这意味着对话框将不再以某种形式存在。因此,当您尝试在对话框中提交输入值时,它们将最终为空,因为没有表单来收集并将输入值发送到服务器。
此外,您只需处理命令按钮内的提交按钮。
<p:commandButton ... process="@this" />
删除该属性。它已经默认为@form
,这正是您想要的。