PrimeFaces可编辑数据表,带有自定义验证器和Omnifaces

时间:2012-11-21 14:29:03

标签: ajax jsf primefaces myfaces omnifaces

我最近将OmniFaces库引入我的项目以利用其Ajax实用程序,但自从这样做以来,我的PrimeFaces可编辑数据表现在忽略了验证错误。

我目前有一个p:datatable,带有自定义验证器和过滤器,如下所示:

<p:dataTable var="ticket" value="#{myBean.tickets}"
    id="ticketTable" widgetVar="ticketTable" editable="true"
    rowKey="#{ticket.idTicket}"
    filteredValue="#{myBean.filteredTickets}"
    paginator="true" rows="20"
    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
    rowsPerPageTemplate="#{myBean.rowsPerPageTemplate}">

    <p:ajax event="rowEdit" listener="#{myBean.onEdit}"
        />
    <p:ajax event="rowEditCancel"
        listener="#{myBean.onCancel}" />


    <p:column headerText="Title" sortBy="#{ticket.title}"
        filterBy="#{ticket.title}">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{ticket.title}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{ticket.title}" />
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column sortBy="#{ticket.start}">
        <f:facet name="header">
            <h:panelGrid columns="1">
                <h:outputText value="Start" />
                <h:panelGrid columns="3">
                    <h:outputLabel value="From:" for="filterTripDateFrom" />
                    <p:calendar id="filterTripDateFrom"
                        value="#{myBean.filterStart}" navigator="true"
                        effect="fadeIn" pattern="MM/dd/yy" size="8">
                        <p:ajax event="dateSelect"
                            listener="#{myBean.filterDates()}"
                            update="ticketTable" />
                    </p:calendar>
                    <p:commandButton value="Clear"
                        action="#{myBean.clearStart()}"
                        update="filterTripDateFrom, ticketTable" />
                </h:panelGrid>
            </h:panelGrid>
        </f:facet>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{ticket.start}">
                    <f:convertDateTime pattern="EE, MMM dd, yyyy:  HH:mm z" />
                </h:outputText>
            </f:facet>
            <f:facet name="input">
                <p:calendar value="#{ticket.start}" pattern="MM/dd/yy HH:mm"
                    stepMinute="15">
                    <f:validator validatorId="dateValidator" />
                    <f:attribute name="endDate"
                        value="#{editEndDate}" />
                </p:calendar>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column sortBy="#{ticket.end}">
        <f:facet name="header">
            <h:panelGrid columns="1">
                <h:outputText value="End" />
                <h:panelGrid columns="3">
                    <h:outputLabel value="To:" for="filterTripDateTo" />
                    <p:calendar id="filterTripDateTo"
                        value="#{myBean.filterEnd}" navigator="true"
                        effect="fadeIn" pattern="MM/dd/yyyy" size="8">
                        <p:ajax event="dateSelect"
                            listener="#{myBean.filterDates()}"
                            update="ticketTable" />
                    </p:calendar>
                    <p:commandButton value="Clear"
                        action="#{myBean.clearEnd()}"
                        update="filterTripDateTo, ticketTable" />
                </h:panelGrid>
            </h:panelGrid>
        </f:facet>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{ticket.end}">
                    <f:convertDateTime pattern="EE, MMM dd, yyyy:  HH:mm z" />
                </h:outputText>
            </f:facet>
            <f:facet name="input">
                <p:calendar value="#{ticket.end}" pattern="MM/dd/yyyy HH:mm"
                    stepMinute="15" binding="#{editEndDate}" />
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Options" style="width:50px">
        <p:rowEditor />
    </p:column>


</p:dataTable>

添加OmniFaces之前的行为是,如果我的自定义日期验证程序(下面提供)抛出ValidatorException,则正在编辑的表中的行将保持打开状态,页面将显示FacesMessage从例外。 添加OmniFaces库后,FacesMessage仍会显示,但表中的行将关闭,就好像没有抛出异常一样。我尝试过使用OmniFaces 1.2&amp; 1.3 SNAPSHOT,两者都有相同的行为 反正有没有取回原来的功能,还是我必须从我的项目中删除OmniFaces?

感谢您的帮助

addl info:Tomcat 7.0; MyFaces 2.1; PrimeFaces 3.4.1; OmniFaces 1.3 SNAPSHOT 20121027

我的自定义日期验证器:

public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException
{
    // get the submitted value for the start date
    DateValidator.logger.debug("starting validation");
    Date startDate = (Date) value;

    // get the bound component that contains the end date
    DateValidator.logger.debug("getting UI component");
    UIInput endDateComponent = (UIInput) component.getAttributes().get(
            "endDate");

    // get the value of the bound component
    DateValidator.logger.debug("getting second date");
    String endDateString = (String) endDateComponent.getSubmittedValue();

    // and parse it into a date
    DateValidator.logger.debug("converting date");
    Date endDate = JodaUtils
            .stringToUtil(endDateString, "MM/dd/yyyy HH:mm");


    // if either of the submitted values were empty, let the required tag
    // take care of it
    if (startDate == null || endDate == null)
    {
        DateValidator.logger.debug("a date was null; start: " + startDate
                + "; end: "
                + endDate);
        return;
    }

    // otherwise if the start time is the same as, or before the end time
    else if (startDate.getTime() >= endDate.getTime())
    {
        DateValidator.logger
                .debug("end date was the same as or before start date; start: "
                        + startDate + "; end: " + endDate);

        // set the bound component as invalid
        endDateComponent.setValid(false);

        // update the container containing the components to show that the
        // fields were invalid
        Ajax.update(endDateComponent.getParent().getClientId());

        // and send a notification to the front end
        throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR,
                "The end time must be after the start time.",
                "The end time must be after the start time."));
    }
    else
    {
        DateValidator.logger.debug("all clear; start: " + startDate
                + "; end: "
                + endDate);
    }
    Ajax.update(":form:ticketTable");
}

1 个答案:

答案 0 :(得分:2)

我能够重现你的问题。这基本上是由MyFaces和OmniPartialResponseWriter的组合引起的。 MyFaces的标准PartialResponseWriter不会将所有方法委托给getWrapped()方法,而是直接委托给本地wrapped变量。在Mojarra中,所有方法都被委托给getWrapped(),因此可以在特定于组件库的PartialResponseWriter实现中覆盖它。

你的代码在Mojarra中运行正常,但是在MyFaces中没有调用PartialResponseWriter#getWrapped()方法(OmniFaces会返回PrimeFaces方法),而是引用了本地wrapped实例(这是如果OmniPartialResponseWriter只有MyFaces拥有一个而不是PrimeFaces,这导致PrimeFaces的PrimePartialResponseWriter被完全跳过。因此,它不能将以下扩展添加到XML响应中,该响应包含JSF验证失败的PrimeFaces ajax引擎的信息。

<extension ln="primefaces" type="args">{"validationFailed":true}</extension>

这是一个相当不幸的问题。此问题已在OmniFaces 1.3中修复。即使不需要实现,解决方案也是generate a bunch of PartialResponseWriter delegate methods anyway(这会破坏整个包装设计模式)。

我不确定是否必须责怪MyFaces没有委托给getWrapped()PartialResponseWriter documentation对此并不够明确,但我觉得这种方法在包装设计模式中是显而易见的。它可以帮助您避免在每个实现中编写/生成一堆委托方法。


无关具体问题,鉴于您已经在使用OmniFaces,您也​​可以使用其<o:validateOrder>代替自定义验证器。

<f:facet name="input">
    <p:calendar id="start" value="#{ticket.start}" pattern="MM/dd/yy HH:mm" stepMinute="15" />
</f:facet>
...
<f:facet name="input">
    <p:calendar id="end" value="#{ticket.end}" pattern="MM/dd/yyyy HH:mm" stepMinute="15" binding="#{editEndDate}" />
    <o:validateOrder components="start end" message="The end time must be after the start time." />
</f:facet>

这基本上就是你所需要的。

对于验证失败时的Ajax#update()方法,我不确定为什么你需要这个,PrimeFaces已经在验证失败的情况下更新整行。如果验证成功,您可能希望在#{myBean.onEdit}后面的方法中调用它。