gpt4 book ai didi

java - primefaces requiredMessage 在 p :autoComplete moved into custom component 时丢失

转载 作者:行者123 更新时间:2023-12-01 14:47:52 24 4
gpt4 key购买 nike

我在一个相当标准的 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>

当我以这种方式运行它时,该组件可以工作 - 如果用户将其留空,或者它没有通过验证,primefaces 边框会变成红色,但 h:message 永远不会显示实际错误(requiredMessage 或在人物转换器)

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

有人遇到过这个吗?我是否做错了什么或设置错了什么?谢谢您的宝贵时间!!

最佳答案

感谢@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"/>

关于java - primefaces requiredMessage 在 p :autoComplete moved into custom component 时丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15236008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com