如何通过验证保留 JSF 视图参数

发布于 2024-12-11 06:56:50 字数 526 浏览 0 评论 0原文

我将 JSF 2.0/CDI 和 PrimeFaces 2.2.1 用于许多 CRUD 表单,这些表单允许用户通过单击数据表中的链接来查看或更新现有实体的属性,其中实体的标识符被传递到CRUD 形式作为视图参数。我在 PrimeFaces InputText 字段中的 CRUD 表单上显示实体的 ID(通常只是一个整数),并将 readonly 属性设置为 true(因为我不能让他们更改它),以便用户知道他们正在编辑哪个实体。 CRUD 表单的支持 bean 是 RequestScoped,除非验证失败,否则它可以正常工作。在这种情况下,视图参数的值会丢失,因此验证失败时实体 ID 字段中会显示 0。

我能够在隐藏字段中维护实际的实体 ID,因此一旦验证成功就可以更新数据库,但令人抓狂的是我无法找到一种方法来维护某种可见字段中的值验证失败后。理想情况下,即使将其只读(或禁用)属性设置为 true,InputText 字段也将保留其作为输入和验证字段的功能,这将使我完全放弃隐藏字段。但我似乎无法让它以这种方式工作。除了制作我希望避免的支持 bean ConversationScoped 之外,还有什么建议吗?

I am using JSF 2.0/CDI and PrimeFaces 2.2.1 for a number of CRUD forms that let the user view or update the attributes of an existing entity by clicking on a link in a datatable, where the identifier of the entity is passed to the CRUD form as a View Parameter. I display the entity's ID (often just an integer) on the CRUD form in a PrimeFaces InputText field with the readonly attribute set to true (since I can't let them change it), so the user knows which entity they're editing. The backing bean of the CRUD form is RequestScoped, which works fine except when validation fails. In that case, the value of the View Parameter is lost, so a 0 is displayed in the entity ID field on validation failure.

I am able to maintain the actual entity ID in a hidden field so it's available to update the database once validation succeeds, but it's rather maddening that I've not been able to find a way to maintain the value in a visible field of some sort after a validation failure. Ideally the InputText field would retain its functionality as an inputted and validated field even with its readonly (or disabled) attribute set to true, which would let me forgo the hidden field entirely. But it doesn't appear that I can make it work that way. Any suggestions besides making the backing bean ConversationScoped, which I'd prefer to avoid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ぺ禁宫浮华殁 2024-12-18 06:56:50

实际上,在以不同的方式陈述了我在谷歌搜索中寻找的内容之后,我在下面的链接中发现了一个新颖的建议,该建议似乎运行良好。我没有将实体 ID 字段设置为只读或禁用,而是将其保留为启用状态,但在获得焦点后立即对其进行模糊处理。我能够摆脱隐藏字段,用户无法更改该值,并且它可以在验证失败后幸存下来。

<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />

http://www.codingforums.com/archive/index.php/t -1738.html

Actually, after stating what I'm looking for a little differently in a Google search I found a novel suggestion at the link below that seems to work cleanly. Instead of making the entity ID field readonly or disabled, I leave it enabled but blur it as soon as it receives focus. I'm able to get rid of the hidden field, the user can't change the value and it survives a validation failure.

<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />

http://www.codingforums.com/archive/index.php/t-1738.html

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