从JSF中的后备bean设置ID [英] Set ID from backing bean in JSF

查看:61
本文介绍了从JSF中的后备bean设置ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这合法吗?

<h:form id="status${a.myID}" >
// ...
</h:form>

其中"a"是备用bean中的对象.似乎有些工作,但是当我查看呈现的HTML时,我看到的id为:例如:0:status,而不是我期望的:status0.我的主要问题是尝试引用<f:ajax render=...中的ID.我几乎可以想到的所有组合都包含包含未知ID ...".是否可以可靠地使用来自支持bean的值来设置ID?

where 'a' is an object in a backing bean. It seems to sort of work, but when I look at the rendered HTML, I see the id as: :0:status for example, instead of :status0 as I would expect. My main problem is trying to reference the id from <f:ajax render=.... I'm getting "contains an unknown id..." with pretty much every combination I can think of. Is it possible to set ids using values from a backing bean reliably?

推荐答案

单字母变量名称${a}和迭代索引(如:0)的症状会自动附加在客户端ID中,表明此包含在JSF迭代组件(例如<h:dataTable><ui:repeat>,其中实际上 不是不是支持bean)的内部.它将确认并解释到目前为止描述的所有症状.如果${a}是真正的支持bean(而不是迭代变量),则它将起作用",并且您会看到:0:status0:1:status0:2:status0等,并且—其有用性值得怀疑.

The single-letter variable name ${a} and the symptom of an iteration index like :0 being auto-appended in client ID, suggests that this <h:form> is inside a JSF iterating component such as <h:dataTable> or <ui:repeat> with a var="a" which actually is not a backing bean. It would confirm and explain all symptoms described so far. If ${a} were a real backing bean (and not an iteration variable), then it would have "worked" and you would have seen :0:status0, :1:status0, :2:status0, etc — whose usefulness is questionable though.

首先,在视图构建期间(即基于XHTML源代码文件构建JSF组件树的那一刻)评估和设置JSF组件的id属性. JSF迭代组件的var属性是在视图呈现时设置的,即基于JSF组件树生成HTML输出的那一刻.因此,逻辑上的结果是,在需要设置id属性时,var属性设置的对象不可用可用,因此计算结果为空/空字符串.效果与您将要执行的操作完全相同

First of all, the id attribute of a JSF component is evaluated and set during view build time, the moment when the JSF component tree is to be built based on XHTML source code file. The var attribue of a JSF iterating component is set during view render time, the moment when the HTML output is to be generated based on JSF component tree. Thus, logical consequence is, the object set by var attribute is not available at the moment the id attribute needs to be set and thus evaluates to null/empty-string. The effect is exactly the same as when you would do

<h:form id="status">

JSF迭代组件即已经将迭代索引自动添加到生成的客户机ID.手动在组件ID中设置迭代项的ID没有任何意义. JSF组件树中实际上只有一个 <h:form>组件,在基于当前迭代回合生成HTML输出的过程中,该组件又被多次重用.

JSF iterating components namely already auto-appends the iteration index to the generated client ID. It would not make any sense to manually set the ID of the iterated item in the component ID. There's namely physically only one <h:form> component in the JSF component tree which is in turn reused multiple times during producing the HTML output based on the current iteration round.

此Q& A还应引起更多思考:JSF2 Facelets中的 JSTL. ..有意义吗?

This Q&A should also give more food for thought: JSTL in JSF2 Facelets... makes sense?

回到引用<f:ajax render>中组件的具体功能要求,您肯定需要以不同的方式解决此问题.不幸的是,您没有清楚地描述源组件和目标组件的上下文,因此不可能提供/说明正确的客户端ID,因此,我只是给您提供一个链接,以便您可以自己确定: 如何查找用于ajax更新/渲染的组件的客户端ID?找不到表达式为"foo"的组件;引用自"bar"

Coming back to your concrete functional requirement of referencing a component in <f:ajax render>, you definitely need to solve this differently. Unfortunately you didn't clearly describe the context of the source component and the target component, so it's impossible to give/explain the right client ID and so I'm just giving you a link so that you can figure it out on your own: How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"

与具体问题无关,旧的JSP EL样式${...}在Facelets中具有与#{...}完全相同的效果.为了避免您自己和您将来的维护者造成混淆,建议完全禁止使用${...}并始终坚持使用#{...}.另请参见 JSP EL,JSF EL和统一EL

Unrelated to the concrete problem, the old JSP EL style ${...} has in Facelets exactly the same effect as #{...}. In order to avoid confusion by yourself and your future maintainers it's recommend to completely ban usage of ${...} and stick to #{...} all the time. See also Difference between JSP EL, JSF EL and Unified EL

这篇关于从JSF中的后备bean设置ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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