Primefaces命令按钮不更新数据表

时间:2015-12-15 03:22:31

标签: jsf-2 primefaces datatable commandbutton

我有primefaces命令按钮多次更新问题。命令按钮位于表单内,而数据表不是。

我试过了:

update=":datalist1,form1" 
update=":datalist1 form1" 
update="datalist1 form1"
update=":#{p:component('datalist1')},form1"

form1每次更新但仍然没有更新数据表

Version info:
primefaces: 5.3 
jsf: org.glassfish 2.3.0-m04
spring: 4.2.3
and I use cdi for controller beans

页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<body>

<h:form id="form1" >
<h:panelGrid columns="3">
<h:outputLabel value="Name: " for="name" />
<p:inputText id="name" value="#{personelController.personel.adi}" required="true" requiredMessage="Name can not be null" />
<p:message id="nameMsg" for="name" />

<h:outputLabel value="Surname: " for="surname" />
<p:inputText id="surname" value="#{personelController.personel.soyadi}" required="true" requiredMessage="Surname can not be null" />
<p:message id="surnameMsg" for="surname" />

<h:outputLabel value="Age: " for="age" />
<p:inputText id="age" value="#{personelController.personel.yasi}" required="true" requiredMessage="Age  can not be null" />
<p:message id="ageMsg" for="age" />

<h:outputLabel value="Job: " for="job" />
<p:inputText id="job" value="#{personelController.personel.meslek}" required="true" requiredMessage="Job  can not be null" />
<p:message id="jobMsg" for="job" />

<h:outputLabel value="City" for="city" />
<p:inputText id="city" value="#{personelController.personel.sehir}" required="true" requiredMessage="City  can not be null" />
<p:message id="cityMsg" for="city" />

<p:commandButton id="add" value="Add" actionListener="#{personelController.createPersonel()}" update=":datalist1,form1" />
</h:panelGrid>
</h:form>
    <p:dataTable id="datalist1" var="person" value="#{personelController.personels}" resizableColumns="true">
        <f:facet name="header">List of Personel</f:facet>
        <p:column headerText="ID">
            <h:outputText value="#{person.id}" />
        </p:column>
        <p:column headerText="Name">
            <h:outputText value="#{person.adi}" />
        </p:column>
        <p:column headerText="Surname">
            <h:outputText value="#{person.soyadi}" />
        </p:column>
        <p:column headerText="Age">
            <h:outputText value="#{person.yasi}" />
        </p:column>
        <p:column headerText="Job">
            <h:outputText value="#{person.meslek}" />
        </p:column>
        <p:column headerText="City">
            <h:outputText value="#{person.sehir}" />
        </p:column>
    </p:dataTable>

</body>
</html>

班级:

@Named
@SpringViewScoped
public class PersonelController implements Serializable{

    private static final long serialVersionUID = 1L;

    @Inject
    private PersonelService personelService;
    private List<Personel> personels;
    private List<Personel> selectedPersonel;
    private Personel personel;

    @PostConstruct
    public void init(){
        personels=personelService.findAll();
        personel=new Personel();
    }

    public PersonelService getPersonelService() {
        return personelService;
    }

    public void setPersonelService(PersonelService personelService) {
        this.personelService = personelService;
    }

    public List<Personel> getPersonels() {
        return personels;
    }

    public void setPersonels(List<Personel> personels) {
        this.personels = personels;
    }

    public List<Personel> getSelectedPersonel() {
        return selectedPersonel;
    }

    public void setSelectedPersonel(List<Personel> selectedPersonel) {
        this.selectedPersonel = selectedPersonel;
    }

    public Personel getPersonel() {
        return personel;
    }

    public void setPersonel(Personel personel) {
        this.personel = personel;
    }

    public void createPersonel(){
        System.out.println("Personel adding..");
        personelService.create(personel);
        personel=new Personel();
        System.out.println("Personel added");
    }

}

我研究过primefaces命令按钮更新,但所有建议都不适用于我。提前谢谢

1 个答案:

答案 0 :(得分:1)

在我看来,新创建的人员从未添加到列表中。所以你必须例如说

personels.add(personel);

personels=personelService.findAll();

在createPersonel()中,或将personel设置为null并在getPersonels()中执行延迟加载。