如果手动输入,自动完成功能不会发送正确的转换器值
这里有一些要点:
- 我有一个 primefaces 自动完成示例,它使用带有自定义转换器的 POJO。
- 我在转换器中返回 getAsString 的 UUID 字符串。
- 如果我通过键盘或鼠标单击从自动完成下拉对话框中选择值,则转换器中的字符串值可以正常发送。
- 但是,当我自己键入值而不从自动完成下拉对话框中进行选择,或者只是将值粘贴到文本字段中时,会出现验证错误:需要值。将会发生错误。
- 我注意到发生此错误是因为发送到服务器端的值不是由我的转换器生成的字符串,而是本身键入的值。
使用 firebug 展示了这一点。
这是当我通过键盘或鼠标单击从自动完成下拉对话框中选择值时,我们可以注意到该值采用 UUID 格式,由我的 pojo 转换器返回:
<input aria-haspopup="true" aria-autocomplete="list" role="textbox"
autocomplete="off" id="DetailDialogForm:Bagian_input"
name="DetailDialogForm:Bagian_input" value="PERSO"
class="ui-inputfield ui-widget ui-state-default ui-corner-all ui-autocomplete-input"
type="text">
<input id="DetailDialogForm:Bagian_hinput"
name="DetailDialogForm:Bagian_hinput"
value="b0019ab2-28c3-451a-90f6-62a6941ffcaa" type="hidden">
这是当我键入或粘贴该值时,我们可以注意到该值正是我输入的值,它不是由我的 pojo 转换器返回的:
<input aria-haspopup="true" aria-autocomplete="list"
role="textbox" autocomplete="off" id="DetailDialogForm:Bagian_input"
name="DetailDialogForm:Bagian_input" value="PERSO"
class="ui-inputfield ui-widget ui-state-default ui-corner-all ui-autocomplete-input"
type="text">
<input id="DetailDialogForm:Bagian_hinput"
name="DetailDialogForm:Bagian_hinput" value="PERSO" type="hidden">
这是该元素的预期行为吗?
我使用 tomcat 7,这些是我的依赖项:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
请分享您的想法,谢谢!
更新
这是 JSF 摘录:
<p:autoComplete id="Bagian" label="Bagian yang di limpahkan"
value="#{tInputBean.activeDetail.map['department']}"
completeMethod="#{tInputBean.filterDepartment}" var="department"
itemLabel="#{department.map['departmentName']}"
itemValue="#{department}" converter="genericConverter"
forceSelection="true" required="true" />
转换器可在 此页面 上找到,在 BalusC 的回答中实体转换器
Here are some points :
- I have a primefaces autocomplete example that makes use of a POJO with a custom converter.
- Im returning a UUID string for the getAsString in the converter.
- The string value from the converter is sent fine if i choose the value from the autocomplete dropdown dialog, either by keyboard or mouse click.
- But when i type the value myself without choosing from the autocomplete dropdown dialog, or just pasted the value into the text field, the Validation Error: Value is required. error will happen.
- I notice this error happens because the value sent to the server side is not the string generated by my converter, but the value typed itself.
Using firebug shows this in action.
This is when the i choose the value from autocomplete dropdown dialog, either by keyboard or mouseclick, we can notice the value is in the UUID format, which is returned by my pojo converter :
<input aria-haspopup="true" aria-autocomplete="list" role="textbox"
autocomplete="off" id="DetailDialogForm:Bagian_input"
name="DetailDialogForm:Bagian_input" value="PERSO"
class="ui-inputfield ui-widget ui-state-default ui-corner-all ui-autocomplete-input"
type="text">
<input id="DetailDialogForm:Bagian_hinput"
name="DetailDialogForm:Bagian_hinput"
value="b0019ab2-28c3-451a-90f6-62a6941ffcaa" type="hidden">
This is when the i typed or pasted the value, we can notice the value is exactly what i typed, which is NOT returned by my pojo converter :
<input aria-haspopup="true" aria-autocomplete="list"
role="textbox" autocomplete="off" id="DetailDialogForm:Bagian_input"
name="DetailDialogForm:Bagian_input" value="PERSO"
class="ui-inputfield ui-widget ui-state-default ui-corner-all ui-autocomplete-input"
type="text">
<input id="DetailDialogForm:Bagian_hinput"
name="DetailDialogForm:Bagian_hinput" value="PERSO" type="hidden">
Is this the intended behaviour of this element ?
Im using tomcat 7, and these are my dependencies :
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
Please share your thoughts, thank you !
UPDATE
This is the JSF excerpt :
<p:autoComplete id="Bagian" label="Bagian yang di limpahkan"
value="#{tInputBean.activeDetail.map['department']}"
completeMethod="#{tInputBean.filterDepartment}" var="department"
itemLabel="#{department.map['departmentName']}"
itemValue="#{department}" converter="genericConverter"
forceSelection="true" required="true" />
And the converter is available at this page, in BalusC's answer on EntityConverter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以发布您的 .xhtml 吗?我需要检查>
请检查:
当用户在autoComplete给出的建议列表上选择时,应该使用use selectListener,并使用onSelectUpdate更新部分视图
检查是否有;在里面尝试删除它
Would you post your .xhtml, i need to check the <p:autoComplete />
Please check:
When user select on list of suggestions given by autoComplete, it should use use selectListener, and update partial view using onSelectUpdate
check if there is <p:ajax/> inside <p:autoComplete/>, try to remove it