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

标签 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 和 1.3 SNAPSHOT,两者具有相同的行为。
是否有办法恢复原始功能,或者我是否必须从我的项目中删除 OmniFaces?

感谢您的帮助

addl 信息:Tomcat 7.0;我的面孔 2.1; PrimeFaces 3.4.1; OmniFaces 1.3 快照 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");
}

最佳答案

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

您的代码在 Mojarra 中运行良好,但在 MyFaces 中 PartialResponseWriter#getWrapped()方法不会被调用(OmniFaces 方法将返回 PrimeFaces 方法),而是调用本地 wrapped 方法实例被引用(在 OmniPartialResponseWriter 的情况下,只是 MyFaces 自己的一个而不是 PrimeFaces 的实例),这导致了 PrimePartialResponseWriter PrimeFaces 的数量被完全跳过。因此,它无法将以下扩展添加到 XML 响应,该响应包含 JSF 验证失败的 PrimeFaces ajax 引擎的信息。

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

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

我不确定我是否应该责怪 MyFaces 没有委托(delegate)给 getWrapped()或不。 PartialResponseWriter documentation对此还不够明确,但对我来说,这种方法在包装设计模式中是显而易见的,这是有道理的。它将使您免于在每个实现中编写/生成一堆委托(delegate)方法。

<小时/>

与具体问题无关,鉴于您已经在使用 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} 后面的方法中调用它相反。

关于ajax - PrimeFaces 可编辑数据表,带有自定义验证器和 Omnifaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13495417/

相关文章:

ajax - Facebook在粘贴url时如何抓取文章正文?

jsf - 为什么 p :resetInput require properties of a managed bean to be set to null first after the form is submitted?

jsf - Primefaces 扩展 11 错误 [...] monacoeditor.EditorOptions 不存在

jsf - 如何使用 JSF 2.0 复合组件实现动态列表?

jsf - Primefaces - 在多文件上传组件中按字母顺序获取文件

java - <p :dialog closeable ="false"> still get closed on pressing Esc

c# - jquery Ajax 响应文本 "There was an error processing the request"

javascript - jquery ajax删除404错误

file-upload - Primefaces 文件上传 allowTypes

javascript - 对 django View 的 Ajax 请求未返回响应