JSF/PrimeFaces对话框中的Javabean验证错误

Sei*_*dis 2 java jsf primefaces bean-validation jsf-2

最近我将我的应用程序更新到JSF 2.1.7和PrimeFaces 3.4.2.当使用以下对话框添加新组时,在保存新组之前,我会收到"名称大小必须介于1到40之间"的验证错误.当我点击选择器的添加按钮时会发生这种情况.我知道显示此消息是因为验证失败.当我添加immediate=true到p:commandButton 时,不会出现验证错误.我不知道是什么触发了验证.

<h:form id="formg" prependId="false">
    <!-- messages -->
    <p:growl id="msgsg" showDetail="true" />

    <!-- data table -->
    <ui:include src="/WEB-INF/flows/groupsTable.xhtml" />

    <p:separator />

    <!-- bottom tool bar -->
    <ui:include src="/WEB-INF/flows/groupsToolBar.xhtml" />

    <!-- preview, edit dialog -->
    <ui:include src="/WEB-INF/flows/groupsDialog.xhtml" />
</h:form>

<p:dialog id="dialogg" header="#{groupsBean.dialogTitle}"
    widgetVar="groupsDialog" dynamic="true" resizable="false" width="800"
height="600" showEffect="fade" hideEffect="fade" modal="true">
    <p:ajax event="close" listener="#{groupsBean.refresh}"
        immediate="true" update=":formg" global="false" process="@this" />

    <p:tabView id="tabPicker">
        <p:tab title="General">
            <h:panelGrid id="displayg" columns="2">
                <h:outputText value="#Group name*:" />
                <p:inputText value="#{groupsBean.selectedGroup.name}" size="40"
                    readonly="#{!groupsBean.updatable}" maxlength="40" />
            </h:panelGrid>
        </p:tab>
        <p:tab title="Members">
            <ui:include src="/WEB-INF/custom/picker.xhtml">
                ... some params passed to picker
            </ui:include>
        </p:tab>
    </p:tabView>
</p:dialog>
Run Code Online (Sandbox Code Playgroud)

选择器类似于<p:password>它,它由两个p:dataTable组件和它们之间的4个按钮组成.这些按钮与ah:panelGrid组合在一起.按钮属性类似.这是按钮示例代码:

<p:outputPanel autoUpdate="true">
    <p:commandButton actionListener="#{eval.evaluateAsMethod(pickerAdd)}"
    update="source, target, #{messages}" immediate="true"
    disabled="#{pickerSourceDisabled}"
    icon="ui-icon ui-icon-arrowthick-1-s" />
</p:outputPanel>
Run Code Online (Sandbox Code Playgroud)

source,target是两个数据表的id.pickerAdd作为参数传递,值为groupsBean.picker.add.这些表包含FooDomain对象.

public class FooDomain implements Serializable {
    ...
    @NotNull  
    @Size(min = 1, max = 40)
    @Column(name = "NAME")
    private String name;
    ...
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 5

PrimeFaces <p:commandButton>默认处理整个表单(如,process="@form"),因此默认情况下会触发所有验证.您的验证错误来自@Size对财产的限制.如果您只想处理按钮自己的操作,那么您应该添加process="@this".

<p:commandButton ... process="@this" />
Run Code Online (Sandbox Code Playgroud)

immediate="true"也可用于解决这个问题,但它在底层的行为有些不同:整个表格还在处理,但该动作在APPLY_REQUEST_VALUES相位而不是INVOKE_ACTION阶段被调用.并且只有已经设置的输入组件immediate="true"将被处理,而其他组件将被跳过.