简体   繁体   English

PrimeFaces Datatable CellEditor-构面输入和输出的不同值

[英]PrimeFaces Datatable CellEditor - Different values for facet input and output

I have an editable DataTable for the entity User. 我有实体User的可编辑DataTable。 This table has a field called username which has an unique constraint 该表具有一个名为username的字段,该字段具有唯一约束

    <h:form id="form">
<p:dataTable id="userTable" value="#{users}" var="entity" editable="true" editMode="cell" tableStyle="width:auto">
<p:ajax event="cellEdit" listener="#{userController.onCellEdit}" update="form:msgs form:display form:userTable" />
<p:column headerText="UserID">
<p:outputLabel value="#{entity.id}" />
</p:column>
<p:column headerText="Username">
<p:cellEditor>
<f:facet name="output"><p:outputLabel for="username" value="#{entity.username}" /></f:facet>
<f:facet name="input">
<p:inputText id="username" value="#{entity.username}" >
<f:validateRequired />
<f:validateLength maximum="50" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>

In the backing bean userController, I have the following method onCellEdit 在支持bean userController中,我具有以下方法onCellEdit

public void onCellEdit(CellEditEvent event) {
        Object newValue = event.getNewValue();
        String columnName = event.getColumn().getHeaderText();
        facesContext = FacesContext.getCurrentInstance();
        User entity = facesContext.getApplication().evaluateExpressionGet(facesContext, "#{entity}", User.class);
        if(columnName.equals("Username") {
            entity.setUsername((String) newValue);
        }


        try {
            service.createOrUpdateEntity(entity);
        }
        catch (EJBTransactionRolledbackException ex) {
            Throwable t = ex.getCause();
            while (t != null) {
                t = t.getCause();
                if(t.getClass().getSimpleName().equals("SQLIntegrityConstraintViolationException")) {
                        entity.setUsername((String) event.getOldValue());
                    facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Duplicate entry for the field username. Write a different username", ""));
                    break;
                }
            }
            return;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

The problem is that when the exception is catched the final value for the facet input is different from the value from the facet output. 问题在于,当捕获到异常时,构面输入的最终值与构面输出的值不同。 In this case the facet output shows the right value while the facet input shows the value that produced the exception. 在这种情况下,构面输出显示正确的值,而构面输入显示产生异常的值。

You need to ensure that the UIInput component has been reset to original values. 您需要确保UIInput组件已重置为原始值。 The UIInput component can be retrieved from the CellEditEvent . 可以从CellEditEvent检索UIInput组件。

((UIInput)event.getComponent()).resetValue();

The above assumes that you've checked the return from getComponent() for null and type safety. 上面的假定您已经检查了getComponent()的返回值是否为null和类型安全。 Here's the API for UIInput 这是UIInput的API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM