JSF 中的组件绑定是什么?何时首选使用? [英] What is component binding in JSF? When it is preferred to be used?
问题描述
我在以下问题中阅读了关于使用 binding
属性的组件绑定:
I have read about component binding with binding
attribute in following questions:
我知道它将视图中 JSF 标记后面的 UI 组件绑定到支持 bean 中的 UIComponent
属性.但是,我很困惑组件绑定的用途是什么以及何时应该使用它.有人可以用更简单的方式解释一下并举一些实际的例子吗?
I understand that it binds the UI component behind the JSF tag in the view to an UIComponent
property in the backing bean. However, I am confused what the use of component binding is and when we should use it. Can someone explain it in a more simpler way and give some practical examples?
推荐答案
如果你想访问 整个 UIComponent
而不仅仅是它的价值.例如,访问一些无法在视图中调用/绑定的方法.这在您发现的第一个问题中得到了回答:JSF 组件绑定 - 一些混乱
You should use it if you want to have access to the entire UIComponent
instead of just only its value. For example, to access some methods which can't be invoked/bound in the view. This is answered in the 1st question you found: JSF component binding - some confusion
您找到的第二个问题,组件绑定与 findComponent() - 什么时候使用?,只回答"binding
vs findComponent()
",不回答"binding
与 value
" 完全如您所想.请不要对此感到困惑.value
显然会战胜 binding
.
The 2nd question which you found, component binding vs findComponent() - when to use which?, merely answers "binding
versus findComponent()
", it does not answer "binding
versus value
" at all as you seem to think. Please don't get confused by this. value
would obviously win over binding
.
在现实世界的代码中,组件绑定到支持 bean 通常仅在开发人员需要以编程方式操作其子级时使用,例如component.getChildren().add(...)
.但是,该 bean 应该是请求范围的.更广泛的范围可能会导致不一致,因为组件实例基本上是在每个请求的基础上创建的,不应该在多个请求之间共享.视图作用域也可以,但是这样内存效率很低,并且在 Mojarra 2.1.18 之前的版本上,部分状态保存也必须关闭,否则 binding
引用的视图作用域 bean 实例会隐式在每个请求上重新创建.另请参阅 JSF2 Facelets 中的 JSTL... 有意义吗?寻找相关答案.
In real world code, component binding to the backing bean is often only used whenever the developer needs to manipulate its children programmatically by e.g. component.getChildren().add(...)
. The bean should however be request scoped. A broader scope may lead to inconsitenties as component instances are basically created on a per-request basis and shouldn't be shared across multiple requests. The view scope can also, but this is very memory inefficient, and on Mojarra versions older than 2.1.18, partial state saving must also be turned off, otherwise the view scoped bean instance referenced by binding
will implicitly be recreated on every request. See also JSTL in JSF2 Facelets... makes sense? for a related answer.
也可以将组件绑定到视图".例如
It's also possible to bind the component to "the view". E.g.
<h:someComponent binding="#{some}">
这指的是 Facelet 范围(页面范围")中的 UIComponent
实例.这使您可以在同一页面的其他位置使用例如 #{some.clientId}
、#{some.value}
.请注意,此处不涉及支持 bean.另请参阅JSF 无 bean 属性的组件绑定.
This refers to an instance of UIComponent
in the Facelet scope ("page scope"). This enables you to use for example #{some.clientId}
, #{some.value}
elsewhere in the same page. Note that no backing bean is involved here. See also JSF component binding without bean property.
这里有一些现实世界使用 binding
属性的工具:
Here are some real world use appliances of binding
attribute:
- 禁用第二个文本通过第一个文本字段的ajax验证数据后的字段
- 检查哪个表单有错误
- 基于下拉列表选择的输入文本验证
- 如何让验证依赖于按下的按钮?
- 如何在 h:dataTable 中实现行编号李>
- 拆分java.util.Date 超过两个 h:inputText 字段,用 f:convertDateTime 表示小时和分钟
这篇关于JSF 中的组件绑定是什么?何时首选使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!