java primefaces:确认对话框按钮未调用 managedbean 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11026973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:29:19  来源:igfitidea点击:

primefaces: confirm dialog button not calling managedbean method

javajsfjsf-2primefaces

提问by mathaker

I am working in primefaces 3.3 and jsf 2.0. I have problem in deleting record in lazy datamodel. As sorting and filtering is not working properly in datatable I had to use lazydatamodel concept.Now save(through wizard), edit,sorting and filtering are working fine. When I tried to delete record from datatable, dialog is appearing and when I click on yes(command button) to delete record, instead of calling bean method control goes to load method of lazy data model. deleterecord of clientUitility.java(managedbean) not calling. can anyone give me guidance to delete record from datatable by passing client object Here is code snippet

我正在使用 primefaces 3.3 和 jsf 2.0。我在删除惰性数据模型中的记录时遇到问题。由于排序和过滤在数据表中无法正常工作,我不得不使用lazydatamodel 概念。现在保存(通过向导)、编辑、排序和过滤工作正常。当我尝试从数据表中删除记录时,会出现对话框,当我单击是(命令按钮)删除记录时,而不是调用 bean 方法控件转到惰性数据模型的加载方法。未调用 clientUtility.java(managedbean) 的 deleterecord。任何人都可以通过传递客户端对象来指导我从数据表中删除记录这是代码片段

clientMaster.xhtml

客户端主.xhtml

 <h:form id="cm">
                <p:growl life="5000" showDetail="true" showSummary="true" id="mymessage" />
                <p:wizard widgetVar="wiz" flowListener="#{clientUitility.onFlowProcess}" showNavBar="true" >
                    <p:tab id="personal" title="Personal" >
.....
.....// tab and columns
.....

                                        <p:commandButton id="addClient" immediate="true" value="Add Client" actionListener="#{clientUitility.save}" oncomplete="wiz.loadStep (wiz.cfg.steps [0], true)" update=":fce:clientList" > <!-- update="@parent,:cm:clientList" -->
                                        </p:commandButton>
                                    </p:column>
                                </p:row>
                            </p:panelGrid>
                        </p:panel>
                    </p:tab>
                </p:wizard>
            </h:form>
            <ui:include id="ce" src="ClientEditDatatable.xhtml"/>

ClientEditdatatable.xhtml

客户端编辑数据表.xhtml

<h:form id="fce">


        <p:dataTable var="client" value="#{clientUitility.lazyModel}" id="clientList" editable="true" widgetVar="clientTable" rowKey="#{client.clientID}"
                     paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                     paginator="true" rows="10" rowsPerPageTemplate="5,10,15" lazy="true">

            <p:column headerText="First Name" style="width:100px" filterBy="#{client.firstName}" sortBy="#{client.firstName}" >
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{client.firstName}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText id="dtFirstName" value="#{client.firstName}" style="width:100%" label="FirstName" onkeyup="valid(this)" onblur="convertUpper(this.id)"  >
                            <f:validateLength for="dtFirstName" maximum="100" minimum="3"/>
                        </p:inputText>
                    </f:facet>
                </p:cellEditor>
            </p:column>
.....
..... // code of column
.....
            <p:ajax event="rowEdit" listener="#{clientUitility.editRowListner}" update=":cm:mymessage"/>
            <p:column headerText="Delete" style="width:50px">
                <p:commandButton id="deleteClient" value="Delete" onclick="confirmDeleteClient.show()" title="Delete this Client" styleClass="ui-icon-trash">
                    <f:setPropertyActionListener target="#{client}" value="#{clientUitility.client}"/>
                </p:commandButton>
            </p:column>
        </p:dataTable>
        <p:confirmDialog message="Delete client?" severity="alert" widgetVar="confirmDeleteClient" visible="false" appendToBody="true" >
            <p:commandButton value="Yes" update="clientList" immediate="true" oncomplete="confirmDeleteClient.hide()" actionListener="#{clientUitility.deleteRecord}" >
<f:setPropertyActionListener target="#{client}" value="#{client}"
            </p:commandButton>
            <p:commandButton value="No" onclick="confirmDeleteClient.hide()" type="button" />
        </p:confirmDialog>
        <br/>

LazyClientDataModel.java

延迟客户端数据模型.java

public class LazyClientDataModel extends LazyDataModel<ClientBean> {
private List<ClientBean> datasource;
public LazyClientDataModel(List<ClientBean> datasource) {
    this.datasource = datasource;
}
@Override
public ClientBean getRowData(String rowKey) {
    for(ClientBean client : datasource) {
        if(Integer.toString(client.getclientID()).equals(rowKey))
            return client;
    }
    return null;
}
@Override
public Object getRowKey(ClientBean client) {
    return Integer.toString(client.getclientID());
}
@Override
public List<ClientBean> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
    List<ClientBean> data = new ArrayList<ClientBean>();
    //filter
    for(ClientBean client : datasource) {
        boolean match = true;
        for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
            try {
                String filterProperty = it.next();
                String filterValue = filters.get(filterProperty);
                String fieldValue = String.valueOf(client.getClass().getField(filterProperty).get(client));
                if(filterValue == null || fieldValue.toLowerCase().startsWith(filterValue.toLowerCase())) {
                    match = true;
                } else {
                    match = false;
                    break;
                }
            } catch(Exception e) {
                match = false;
            }
        }
        if(match) {
            data.add(client);
        }
    }
    //sort
    if(sortField != null) {
        Collections.sort(data, new LazySorter(sortField, sortOrder));
    }
    //rowCount
    int dataSize = data.size();
    this.setRowCount(dataSize);
    //paginate
    if(dataSize > pageSize) {
        try {
            return data.subList(first, first + pageSize);
        }
        catch(IndexOutOfBoundsException e) {
            return data.subList(first, first + (dataSize % pageSize));
        }
    }else {
        return data;
    }
}
}

clientUitility.java (managedbean)

clientUtility.java(托管bean)

public class ClientUitility {
private LazyDataModel<ClientBean> lazyModel;
private ClientBean client = new ClientBean();
private List<ClientBean> clientAll;// = new ArrayList<ClientBean>();

/** Creates a new instance of ClientUitility */
public ClientUitility() {
    client = new ClientBean();
    clientAll = new ArrayList<ClientBean>();
    //int userID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("USER_ID").toString());
    clientAll = ClientService.GenerateClientList(1);
    lazyModel = new LazyClientDataModel(clientAll);
}
// setter and getter of client 
// getter for lazymodel
public void deleteRecord(ActionEvent actionEvent) {
    try {
        System.out.println("Delete record Called....");
        int ClientID = client.getclientID();            
        ClientService.DeleteClient(client);
        client = new ClientBean();
        clientAll = ClientService.GenerateClientList(1);
        lazyModel = new LazyClientDataModel(clientAll);
        FacesMessage msg = new FacesMessage("Client Deleted", "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
public void save(ActionEvent actionEvent) {
    System.out.println("Save record Called....");
    ClientService.AddClient(client);
    client = new ClientBean();
    clientAll = ClientService.GenerateClientList(1);
    lazyModel = new LazyClientDataModel(clientAll);
    FacesMessage msg = new FacesMessage("Client Created", "");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}
}

采纳答案by mathaker

I have changed PF 3.2 instead of PF 3.3 and it's work smoothly. Check this link: http://forum.primefaces.org/viewtopic.php?f=3&t=22216. Its bug in PF3.3. No need to change any thing in my project.

我已经更改了 PF 3.2 而不是 PF 3.3 并且它工作顺利。检查此链接:http: //forum.primefaces.org/viewtopic.php?f=3&t=22216。它在 PF3.3 中的错误。无需更改我的项目中的任何内容。

回答by Remy

This is expected behaviour, since you are using appendToBody="true". You should add a form inside your confirm dialog.

这是预期的行为,因为您使用的是 appendToBody="true"。您应该在确认对话框中添加一个表单。

<p:confirmDialog message="Delete client?" severity="alert" widgetVar="confirmDeleteClient" visible="false" appendToBody="true" >
  <h:form>
    <p:commandButton value="Yes" update="clientList" immediate="true" oncomplete="confirmDeleteClient.hide()" actionListener="#{clientUitility.deleteRecord}" >
      <f:setPropertyActionListener target="#{client}" value="#{client}"
    </p:commandButton>
   <p:commandButton value="No" onclick="confirmDeleteClient.hide()" type="button" />
  </h:form>
</p:confirmDialog>

回答by Giuseppe Matheus

remove atributte type="button"

删除属性类型=“按钮”

回答by ARny

You can also use remoteCommand component defined outside of confirmDialog, which will be still inside form (when you use appendToBody=true).

您还可以使用在confirmDialog 之外定义的remoteCommand 组件,该组件仍将在表单内(当您使用appendToBody=true 时)。