我在一个非常标准的Java Web应用程序(jsf-2)中使用PrimeFaces,当我将p: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:消息?这是完美的,当值为空时显示,或者它没有通过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>
当我以这种方式运行时,组件工作 - 如果用户将其留空,或者它确实通过vaildation,则primefaces边框变为红色,但h:消息从不显示实际错误(requiredMessage或在PersonConverter)
我尝试了很多不同的变体,比如只在实现中设置id,或者只在my:personLookup中设置id,或者两者都设置。
以前有人遇到过这个吗?我在做什么或设置错误的东西?谢谢你的时间!!
答案 0 :(得分: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"/>