如何通过验证持久化JSF视图参数 [英] How to persist JSF view parameters through validation

查看:94
本文介绍了如何通过验证持久化JSF视图参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我能够在隐藏字段中维护实际的实体ID,因此一旦验证成功就可以用来更新数据库,但是令人惊讶的是,我无法找到一种在可见字段中维护值的方法验证失败后的某种形式.理想情况下,即使将其readonly(或disable)属性设置为true,InputText字段仍将保留其功能,作为输入并经过验证的字段,这将让我完全放弃隐藏字段.但似乎我无法使其那样工作.除了制作支持bean ConversationScoped之外,还有其他建议吗?

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?

推荐答案

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

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

这篇关于如何通过验证持久化JSF视图参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆