JSF 中的组件绑定是什么?什么时候优先使用? [英] What is component binding in JSF? When it is preferred to be used?

查看:23
本文介绍了JSF 中的组件绑定是什么?什么时候优先使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下问题中阅读了使用 binding 属性的组件绑定:

我知道它将视图中 JSF 标记后面的 UI 组件绑定到支持 bean 中的 UIComponent 属性.但是,我很困惑组件绑定的用途是什么以及何时应该使用它.有人可以用更简单的方式解释它并举一些实际的例子吗?

解决方案

如果你想访问整个 UIComponent 而不仅仅是它的价值.例如,访问一些不能在视图中调用/绑定的方法.这在您发现的第一个问题中得到了回答:JSF 组件绑定 - 一些混淆

您发现的第二个问题,组件绑定与 findComponent() - 何时使用哪个?,仅回答 "bindingfindComponent()",它不回答 "binding vs value" 就像你想象的那样.请不要对此感到困惑.value 显然会战胜 binding.

在现实世界的代码中,组件绑定到支持 bean 通常只在开发人员需要以编程方式操作其子代时使用,例如component.getChildren().add(...).然而,bean 应该是请求范围的.更广泛的范围可能会导致不一致,因为组件实例基本上是基于每个请求创建的,不应跨多个请求共享.视图范围也可以,但是这样内存效率非常低,而且在 2.1.18 之前的 Mojarra 版本上,部分状态保存也必须关闭,否则 binding 引用的视图范围的 bean 实例将隐式在每个请求上重新创建.另请参见 JSF2 Facelets 中的 JSTL ......有意义吗?相关答案.

也可以将组件绑定到视图".例如

这指的是 Facelet 范围(页面范围")中的 UIComponent 实例.这使您可以在同一页面的其他位置使用例如 #{some.clientId}#{some.value}.请注意,此处不涉及任何支持 bean.另请参阅没有 bean 属性的 JSF 组件绑定.

以下是一些现实世界中使用 binding 属性的设备:

I have read about component binding with binding attribute in following questions:

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?

解决方案

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

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.

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}">

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.

Here are some real world use appliances of binding attribute:

这篇关于JSF 中的组件绑定是什么?什么时候优先使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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