没有 bean 属性的 JSF 组件绑定

发布于 2024-12-16 02:09:06 字数 784 浏览 4 评论 0原文

下面的代码到底是如何工作的:

#{aaa.id}
<h:inputText id="txt1" binding="#{aaa}"/>

我的意思是,通常组件绑定是通过在 bean 中指定一个属性(UIComponent 类型)来工作的。在这里,没有 bean 也没有属性,但名称“aaa”仍然被正确绑定(显示组件 id -“txt1”)。它是如何工作的/在哪里指定的?

谢谢

更新:JSF2.0 规范 [pdf](第 3.1.5 章)说:

“组件绑定是一种特殊的值表达式,可用于促进将组件实例“连接”到 JavaBean 的相应属性...指定的 ValueExpression 必须指向 UIComponent 类型的读写 JavaBeans 属性(或 适当的子类)。”

How does exactly the following code work:

#{aaa.id}
<h:inputText id="txt1" binding="#{aaa}"/>

I mean, usually the component binding works, by specifying a property (of type UIComponent) in a bean. Here, there's no bean nor property but nevertheless the name "aaa" gets bound correctly (displaying the component id - "txt1"). How does it work/where is it specified?

Thanks

UPDATE: The JSF2.0 Spec [pdf] (Chapter 3.1.5) says:

"A component binding is a special value expression that can be used to facilitate “wiring up” a component instance to a
corresponding property of a JavaBean... The specified ValueExpression must point to a read-write JavaBeans property of type UIComponent (or
appropriate subclass)."

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

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

发布评论

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

评论(1

﹏雨一样淡蓝的深情 2024-12-23 02:09:06

在构建视图树期间,它已被放入默认的 EL 作用域中(即所有 绑定 属性以及标记处理程序的属性,如 JSTL 和JSF -- 正在评估)。它在视图树渲染期间通过普通 EL 方式显示。视图树的渲染发生在视图树构建之后,因此它是这样工作的。这段代码并不是像您从源代码中期望的那样“逐行”运行。

我无法向您指出指定的单个参考文献,因为没有。您必须阅读 EL 规范JSF 规范 单独进行 1+1=2。

顺便说一句,为了避免新开发人员感到困惑并避免与 EL 范围中的现有变量发生冲突,您可以使用 java.util.HashMap 在请求范围内,在 faces-config.xml:

<managed-bean>
    <description>Holder of all component bindings.</description>
    <managed-bean-name>components</managed-bean-name>
    <managed-bean-class>java.util.HashMap</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

使用

#{components.aaa.id}
<h:inputText id="txt1" binding="#{components.aaa}"/>

并按如下方式 更多自我记录。

另请参阅:

It's been put in the default EL scope during building of the view tree (that's when all binding attributes -- and attributes of tag handlers like JSTL <c:xxx> and JSF <f:xxx> -- are being evaluated). It's being shown by normal EL means during rendering of the view tree. Rendering of the view tree happens after building of the view tree, so it works that way. It's not that this code runs "line by line" as you seemed to expect from the source.

I can't point you out a single reference where it's been specified as there is none. You'd have to read both the EL spec and JSF spec separately and do a 1+1=2.

By the way, to avoid confusion among new developers and to avoid clashes with existing variables in the EL scopes, you can use a java.util.HashMap in the request scope which is been declared as follows in faces-config.xml:

<managed-bean>
    <description>Holder of all component bindings.</description>
    <managed-bean-name>components</managed-bean-name>
    <managed-bean-class>java.util.HashMap</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

and is been used as follows

#{components.aaa.id}
<h:inputText id="txt1" binding="#{components.aaa}"/>

which is more self-documenting.

See also:

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