如果手动输入,自动完成功能不会发送正确的转换器值

发布于 2024-11-06 06:23:18 字数 2683 浏览 3 评论 0原文

这里有一些要点:

  1. 我有一个 primefaces 自动完成示例,它使用带有自定义转换器的 POJO。
  2. 我在转换器中返回 getAsString 的 UUID 字符串。
  3. 如果我通过键盘或鼠标单击从自动完成下拉对话框中选择值,则转换器中的字符串值可以正常发送。
  4. 但是,当我自己键入值而不从自动完成下拉对话框中进行选择,或者只是将值粘贴到文本字段中时,会出现验证错误:需要值。将会发生错误。
  5. 我注意到发生此错误是因为发送到服务器端的值不是由我的转换器生成的字符串,而是本身键入的值。

使用 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 :

  1. I have a primefaces autocomplete example that makes use of a POJO with a custom converter.
  2. Im returning a UUID string for the getAsString in the converter.
  3. 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.
  4. 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.
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩耳倾听 2024-11-13 06:23:18

您可以发布您的 .xhtml 吗?我需要检查>

请检查:

  1. 您是否添加了属性forceSelection
  2. 当用户在autoComplete给出的建议列表上选择时,应该使用use selectListener,并使用onSelectUpdate更新部分视图

  3. 检查是否有;在里面尝试删除它

Would you post your .xhtml, i need to check the <p:autoComplete />

Please check:

  1. do you have added <p:autoComplete> attribute forceSelection
  2. When user select on list of suggestions given by autoComplete, it should use use selectListener, and update partial view using onSelectUpdate

  3. check if there is <p:ajax/> inside <p:autoComplete/>, try to remove it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文