更新p:dataTable中的一行

时间:2014-12-15 15:14:05

标签: jsf jsf-2 primefaces

我正在使用JSF和primefaces。我想点击按钮(铅笔)时更新一行。我使用了本教程中的第一个代码(Primefaces docs),但是在我的情况下有效。 问题是:当我点击按钮(铅笔)更新一行时,没有任何变化。我总是有旧的价值观

这是我的JSF代码:

<f:view>
    <h:form id="form">
        <p:growl id="msgs" showDetail="true"/>


        <p:dataTable id="tservice" var="item" value="#{serviceMBean.services}" editable="true" style="margin-bottom:20px">
            <f:facet name="header"> edit </f:facet>
            <p:ajax event="rowEdit" listener="#{serviceMBean.onRowEdit}" update=":form:msgs" />
            <p:ajax event="rowEditCancel" listener="#{serviceMBean.onRowCancel}" update=":form:msgs" />
            <p:column headerText="Libelle du service">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{item.libelleservice}" /></f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{item.libelleservice}" style="width:100%" label="Libelle"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Utilisateurs/Service">
                <p:cellEditor>
                    <f:facet name="output"><h:outputText value="#{item.nbruserservice}" /></f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{item.nbruserservice}" style="width:100%" label="Nbre Users"/>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column style="width:32px">
                <p:rowEditor />

            </p:column>

这是我的托管bean代码:

@Named(value = "serviceMBean")
@ViewScoped
public class ServiceMBean implements Serializable {

    private List<Service> serviceList;
    private Service s;

    @EJB
    private ServiceManager serviceManager;

    public ServiceMBean() {}

    public void modifierService(Service s1) {
        this.serviceManager.updateService(s1);
    }

    public void onRowEdit(RowEditEvent event) {
        Service s2 = (Service) event.getObject();
        System.out.println("--->" + s2.getNbruserservice());
        FacesMessage msg = new FacesMessage("Service Modifié", ((Service) event.getObject()).getLibelleservice());
        this.modifierService(s2);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Modification annulée", ((Service) event.getObject()).getLibelleservice());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

这是我ServiceManager的代码:

public void updateService(Object service) {
    em.merge(service);
}

我认为我的JSF页面中的setter不起作用。任何解决方案或建议?

1 个答案:

答案 0 :(得分:0)

解决方案:使用@PostConstruct添加void方法。

示例:@PostConstruct

@PostConstruct
public void initialiser() {
    serviceList = this.getServices();
}
  1. 不要忘记serviceList
  2. 的吸气剂和制定者
  3. 在JSF页面中,datatable的值为:value="#{ServiceMBean.serviceList}"