inputText 如何设置 bean 的属性?
How an inputText set an attribute from the bean?
我试图创建一个对话框,我可以插入一个值以用于我的 bean。但是,我的 InputText 没有更新 Bean 上的对象。
这是我的输入文本:
我的 inputText 缺少什么?我试过@this 和 h:param 但我什么也没得到。
这是我的 xhtml,基本上我添加了一个对话框来插入新对象:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Composition para poder importar template -->
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/templates/modeloSistema.xhtml">
<ui:define name="menu">
<ui:include src="/includes/menuPrincipal.xhtml"></ui:include>
</ui:define>
<ui:define name="conteudo">
<h:form>
<p:dataTable emptyMessage="Nenhum Fabricante registrado"
value="#{MBFabricante.itens}" var="item" paginator="true" rows="10">
<f:facet name="header">
Fabricante - Listagem
</f:facet>
<p:column headerText="Codigo" sortBy="#{item.codigo}"
filterBy="#{item.codigo}">
<p:outputLabel value="#{item.codigo}"></p:outputLabel>
</p:column>
<p:column headerText="Descricao" sortBy="#{item.descricao}"
filterBy="#{item.descricao}">
<p:outputLabel value="#{item.descricao}"></p:outputLabel>
</p:column>
<f:facet name="footer">
<!-- Utilizar complete para fazer apos a criacao de instanciar fabricante com prepararNovo -->
<p:commandButton value="Novo" process="@this"
actionListener="#{MBFabricante.prepararNovo}"
oncomplete="PF('dlgFabNovo').show();" />
</f:facet>
</p:dataTable>
</h:form>
<!-- O @(body) serve para indicar que o modal deve exercer funcao sobre o modal -->
<p:dialog widgetVar="dlgFabNovo" closable="true" draggable="true"
resizable="false" modal="true" appendTo="@(body)"
header="Fabricante - Novo">
<h:form>
<h:panelGrid columns="2">
<p:outputLabel for="descricao" value="Descricao: "></p:outputLabel>
<p:inputText id="descricao"
value="#{MBFabricante.fabricante.descricao}"></p:inputText>
</h:panelGrid>
<!-- action usa metodos criados no manage bean utilizando comandos l # -->
<p:commandButton value="Gravar" process="@this"
actionListener="#{MBFabricante.novo}"></p:commandButton>
<p:commandButton value="Cancelar" onclick="PF('dlgFabNovo').hide();"></p:commandButton>
</h:form>
</p:dialog>
</ui:define>
</ui:composition>
这是我的 Bean,如果你们认为有必要,我可以添加我的域:
package br.com.drogaria.bean;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.ListDataModel;
import br.com.drogaria.dao.FabricanteDAO;
import br.com.drogaria.domain.Fabricante;
@ManagedBean(name = "MBFabricante")
@ViewScoped
public class FabrincanteBean {
private Fabricante fabricante;
private ListDataModel<Fabricante> itens;
public ListDataModel<Fabricante> getItens() {
System.out.println("Passou 1");
return itens;
}
public void setItens(ListDataModel<Fabricante> itens) {
System.out.println("Passou 12");
this.itens = itens;
}
public Fabricante getFabricante() {
System.out.println("Passou 2");
return fabricante;
}
public void setFabricante(Fabricante fabricante) {
System.out.println("Passou 23");
this.fabricante = fabricante;
}
// Post , esse metodo vai ser chamado antes da pagina ser desenhada.
@PostConstruct
public void prepararPesquisa() {
try {
FabricanteDAO dao = new FabricanteDAO();
ArrayList<Fabricante> lista;
lista = dao.listar();
// Converte Arraylist para DataModel
itens = new ListDataModel<Fabricante>(lista);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void prepararNovo() {
fabricante = new Fabricante();
System.out.println("Preparou fabricante " + fabricante);
}
public void novo() {
System.out.println("Passou pelo metodo novo, valor " + fabricante);
try {
FabricanteDAO dao = new FabricanteDAO();
dao.salvar(fabricante);
prepararPesquisa();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<p:commandButton value="Gravar" process="@this" actionListener="#{MBFabricante.novo}"></p:commandButton>`
将不会处理 inputText,因为您选择了 @this
只处理 commandButton。尝试 process="@form"
当您按下 commandButton 时,这应该包括来自 inputText 的值。
PrimeFaces Showcase 上完美地展示了其工作原理,您可以在其中尝试不同的过程属性,https://www.primefaces.org/showcase/ui/ajax/process.xhtml
这里还有一个之前的 Q/A 进一步介绍了这一点,Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
我试图创建一个对话框,我可以插入一个值以用于我的 bean。但是,我的 InputText 没有更新 Bean 上的对象。
这是我的输入文本:
我的 inputText 缺少什么?我试过@this 和 h:param 但我什么也没得到。
这是我的 xhtml,基本上我添加了一个对话框来插入新对象:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Composition para poder importar template -->
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/templates/modeloSistema.xhtml">
<ui:define name="menu">
<ui:include src="/includes/menuPrincipal.xhtml"></ui:include>
</ui:define>
<ui:define name="conteudo">
<h:form>
<p:dataTable emptyMessage="Nenhum Fabricante registrado"
value="#{MBFabricante.itens}" var="item" paginator="true" rows="10">
<f:facet name="header">
Fabricante - Listagem
</f:facet>
<p:column headerText="Codigo" sortBy="#{item.codigo}"
filterBy="#{item.codigo}">
<p:outputLabel value="#{item.codigo}"></p:outputLabel>
</p:column>
<p:column headerText="Descricao" sortBy="#{item.descricao}"
filterBy="#{item.descricao}">
<p:outputLabel value="#{item.descricao}"></p:outputLabel>
</p:column>
<f:facet name="footer">
<!-- Utilizar complete para fazer apos a criacao de instanciar fabricante com prepararNovo -->
<p:commandButton value="Novo" process="@this"
actionListener="#{MBFabricante.prepararNovo}"
oncomplete="PF('dlgFabNovo').show();" />
</f:facet>
</p:dataTable>
</h:form>
<!-- O @(body) serve para indicar que o modal deve exercer funcao sobre o modal -->
<p:dialog widgetVar="dlgFabNovo" closable="true" draggable="true"
resizable="false" modal="true" appendTo="@(body)"
header="Fabricante - Novo">
<h:form>
<h:panelGrid columns="2">
<p:outputLabel for="descricao" value="Descricao: "></p:outputLabel>
<p:inputText id="descricao"
value="#{MBFabricante.fabricante.descricao}"></p:inputText>
</h:panelGrid>
<!-- action usa metodos criados no manage bean utilizando comandos l # -->
<p:commandButton value="Gravar" process="@this"
actionListener="#{MBFabricante.novo}"></p:commandButton>
<p:commandButton value="Cancelar" onclick="PF('dlgFabNovo').hide();"></p:commandButton>
</h:form>
</p:dialog>
</ui:define>
</ui:composition>
这是我的 Bean,如果你们认为有必要,我可以添加我的域:
package br.com.drogaria.bean;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.ListDataModel;
import br.com.drogaria.dao.FabricanteDAO;
import br.com.drogaria.domain.Fabricante;
@ManagedBean(name = "MBFabricante")
@ViewScoped
public class FabrincanteBean {
private Fabricante fabricante;
private ListDataModel<Fabricante> itens;
public ListDataModel<Fabricante> getItens() {
System.out.println("Passou 1");
return itens;
}
public void setItens(ListDataModel<Fabricante> itens) {
System.out.println("Passou 12");
this.itens = itens;
}
public Fabricante getFabricante() {
System.out.println("Passou 2");
return fabricante;
}
public void setFabricante(Fabricante fabricante) {
System.out.println("Passou 23");
this.fabricante = fabricante;
}
// Post , esse metodo vai ser chamado antes da pagina ser desenhada.
@PostConstruct
public void prepararPesquisa() {
try {
FabricanteDAO dao = new FabricanteDAO();
ArrayList<Fabricante> lista;
lista = dao.listar();
// Converte Arraylist para DataModel
itens = new ListDataModel<Fabricante>(lista);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void prepararNovo() {
fabricante = new Fabricante();
System.out.println("Preparou fabricante " + fabricante);
}
public void novo() {
System.out.println("Passou pelo metodo novo, valor " + fabricante);
try {
FabricanteDAO dao = new FabricanteDAO();
dao.salvar(fabricante);
prepararPesquisa();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<p:commandButton value="Gravar" process="@this" actionListener="#{MBFabricante.novo}"></p:commandButton>`
将不会处理 inputText,因为您选择了 @this
只处理 commandButton。尝试 process="@form"
当您按下 commandButton 时,这应该包括来自 inputText 的值。
PrimeFaces Showcase 上完美地展示了其工作原理,您可以在其中尝试不同的过程属性,https://www.primefaces.org/showcase/ui/ajax/process.xhtml
这里还有一个之前的 Q/A 进一步介绍了这一点,Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes