0

我在一个非常标准的 Java Web 应用程序(jsf-2)中使用 PrimeFaces,当我将 ap:autoComplete 组件移动到它自己的自定义组件中时,我的所需消息显示出现问题。

首先,当它在页面本身上时 p:autoComplete - 这工作正常(简化):

<h:panelGrid  id="newRoleAdminGrid" >

            <p:autoComplete id="newRoleAdminPerson"
                            style="position: relative; padding-right:5px"
                            value="#{roleList.newRoleAdminPerson}"
                            var="personVar" 
                            itemValue="#{personVar}" 
                            itemLabel="#{personVar.fullFamiliarName}"
                            forceSelection="true"
                            completeMethod="#{personAutoComplete.byGeneralCriteria}"
                            converter="PersonConverter"
                            minQueryLength="3"
                            maxResults="50" 
                            scrollHeight="400"
                            size="50"
                            required="true" 
                            requiredMessage="admin name is requiredd"
                            title="enter users first name, last name or login" >
            </p:autoComplete> 

            <h:message for="newRoleAdminPerson" class="redError"/>

            <p:commandButton value="add" ajax="true" 
                             update="roleAdminTable,newRoleAdminGrid"
                             id="addRoleAdminBtn"
                             actionListener="#{roleList.newRoleAdmin}"
                             style="margin:5px;"
                             icon="ui-icon-plus"/>
        </h:panelGrid>

看到上面的 h:message 了吗?当值为空白或未通过 PersonConverter 时,它可以完美运行并显示...

但是我的应用程序中到处都有相同的组件,所以我创建了一个自定义组件。

这是组件:

<cc:interface>
    <cc:attribute name="id" type="String" required="true" />
    <cc:attribute name="person" type="com.foo.bar.model.Person" required="true" />
    <cc:attribute name="size" type="int" required="false" default="50" />
    <cc:attribute name="required" type="boolean" required="false" default="false" />
    <cc:attribute name="requiredMessage" type="String" required="false" default="required" />
</cc:interface>

<!-- IMPLEMENTATION -->

<cc:implementation>

    <p:autoComplete id="#{cc.attrs.id}"
                    style="position: relative; padding-right:5px"
                    value="#{cc.attrs.person}"
                    var="personVar" 
                    itemValue="#{personVar}" 
                    itemLabel="#{personVar.fullFamiliarName}"
                    forceSelection="true"
                    completeMethod="#{personAutoComplete.byGeneralCriteria}"
                    converter="PersonConverter"
                    minQueryLength="3"
                    maxResults="50" 
                    scrollHeight="400"
                    size="#{cc.attrs.size}"
                    required="#{cc.attrs.required}" 
                    requiredMessage="#{cc.attrs.requiredMessage}"
                    title="enter users first name, last name or login" >
    </p:autoComplete>

    <h:message for="#{cc.attrs.id}" class="redError"/>

</cc:implementation>

这是现在的代码(使用自定义组件):

<h:panelGrid  id="newRoleAdminGrid" >

            <my:personLookup id="newRoleAdminPerson"  
                             person="#{roleList.newRoleAdminPerson}"
                             required="true" requiredMessage="#{propUtil.appShortName} Admin Name is required"/>

            <h:message for="newRoleAdminPerson" class="redError"/>

            <p:commandButton value="add" ajax="true" 
                             update="roleAdminTable,newRoleAdminGrid"
                             id="addRoleAdminBtn"
                             actionListener="#{roleList.newRoleAdmin}"
                             style="margin:5px;"
                             icon="ui-icon-plus"/>

        </h:panelGrid>

当我以这种方式运行它时,组件可以工作 - 如果用户将其留空,或者它没有通过验证,primefaces 边框会变为红色,但 h:message 永远不会显示实际错误(requiredMessage 或 PersonConverter 中设置的内容)

我已经尝试了很多不同的变体,例如仅在实现中设置 id 或仅在 my:personLookup 中设置 id,或两者兼而有之。

有没有人遇到过这个?我在做或设置不正确吗?谢谢你的时间!!

4

1 回答 1

1

感谢@partlov 的解决方案。这是我正确显示消息的最终代码-

组件:

<!-- INTERFACE -->

<cc:interface>
    <cc:attribute name="person" type="com.foo.bar.model.Person" required="true" />
    <cc:attribute name="size" type="int" required="false" default="50" />
    <cc:attribute name="required" type="boolean" required="false" default="false" />
    <cc:attribute name="requiredMessage" type="String" required="false" default="required" />
</cc:interface>


<!-- IMPLEMENTATION -->

<cc:implementation>

    <p:autoComplete id="personAutoComplete"
                    style="position: relative; padding-right:5px"
                    value="#{cc.attrs.person}"
                    var="personVar" 
                    itemValue="#{personVar}" 
                    itemLabel="#{personVar.fullFamiliarName}"
                    forceSelection="true"
                    completeMethod="#{personAutoComplete.byGeneralCriteria}"
                    converter="PersonConverter"
                    minQueryLength="3"
                    maxResults="50" 
                    scrollHeight="400"
                    size="#{cc.attrs.size}"
                    required="#{cc.attrs.required}" 
                    requiredMessage="#{cc.attrs.requiredMessage}"
                    title="enter users first name, last name or login" >
    </p:autoComplete>

    <h:message for="personAutoComplete" errorClass="redError" class="redError"/>

</cc:implementation>

在我的 xhtml 中调用:

<my:personLookup id="newRoleAdminPerson"  
                     person="#{roleList.newRoleAdminPerson}"
                     required="true" requiredMessage="Admin Name is required"/>
于 2013-03-06T16:54:58.263 回答