绑定属性导致在视图中发现重复的组件 ID [英] Binding attribute causes duplicate component ID found in the view
问题描述
这是JSF代码:
<h:inputText binding="#{bean.input}" />
这里是用于绑定支持的支持 bean 的一部分:
And here is a part of backing bean for binding support:
private HtmlInputText input;
public void setInput(HtmlInputText input) {
this.input = input;
}
public HtmlInputText getInput() {
return this.input;
}
当我第一次打开页面时一切正常,但是当我第二次打开它时(在另一个选项卡或任何其他方式中刷新或打开相同的网址),我收到重复的 ID 错误.错误消息指出
没有唯一 ID.这是长错误消息的一部分:
When I open page at first time everything works fine but when I open it at second time (refresh or open the same url in another tab or any other way) I get duplicate ID error. Error message says that <h:inputText>
has no unique ID. Here is a part of long error message:
java.lang.IllegalArgumentException: Component ID formId:inputId has already been found in the view
+id: inputId type: javax.faces.component.html.HtmlInputText@cafebabe
问题发生在我添加了 binding
属性之后.如果我删除它,一切都会再次正常工作.如何正确使用 binding
属性?
The problem occured after I added binding
attribute. If I remove it, everything will work fine again. How do I properly use binding
attribute?
推荐答案
在以下情况下可能会出现重复的组件 ID 错误:
Duplicate component ID errors may occur when:
- 同一个
NamingContainer
内的不同组件使用相同的 ID. - 物理上不同的组件绑定到同一个 bean 的相同属性.
是在包含页面而不是父页面中声明的.- 同一个包含页面在同一个
NamingContainer
中被多次包含. - 组件是动态创建的,没有分配显式 ID.
- Same ID is used on different components inside the same
NamingContainer
. - Physically different components are bound to the same property of the same bean.
- The
<f:subview>
is been declared in the include page instead of the parent page. - The same include page is included multiple times inside the same
NamingContainer
. - A component is been dynamically created without having an explicit ID assigned.
这里,NamingContainer
包括
、
和
.
使用binding
时,您应该将其绑定到相关组件在每个请求的基础上独占使用的属性.您的具体情况表明此绑定由多个组件共享,可能跨不同请求共享.当您将组件绑定到支持 bean 的属性时,支持 bean 绝对不应在比请求范围更广的范围内.另请参阅 JSF 2.0 规范第 3.1 章.5(强调我的):
When using binding
, you should bind it to a property which is used exclusively by the component in question on a per-request basis. Your specific case indicates that this binding is been shared by multiple components, perhaps across different requests. When you bind the component to a property of a backing bean, then the backing bean should absolutely not be in a broader scope than the request scope. See also JSF 2.0 specitication chapter 3.1.5 (emphasis mine):
...
组件绑定通常与通过托管 Bean 创建工具动态实例化的 JavaBean 结合使用(请参见第 5.8.1 节VariableResolver 和默认 VariableResolver").强烈建议应用程序开发人员将组件绑定表达式指向的托管 bean 放置在请求"范围内.这是因为将其放置在会话或应用程序范围内需要线程安全,因为 UIComponent 实例取决于在单个线程内运行.将组件绑定放置在会话"范围内时,还会对内存管理产生潜在的负面影响.
Component bindings are often used in conjunction with JavaBeans that are dynamically instantiated via the Managed Bean Creation facility (see Section 5.8.1 "VariableResolver and the Default VariableResolver"). It is strongly recommend that application developers place managed beans that are pointed at by component binding expressions in "request" scope. This is because placing it in session or application scope would require thread-safety, since UIComponent instances depends on running inside of a single thread. There are also potentially negative impacts on memory management when placing a component binding in "session" scope.
另见:
- 绑定"属性在 JSF 中是如何工作的?何时以及如何使用?
- 如何在 JSF 中使用组件绑定对吗?(会话作用域 bean 中的请求作用域组件)
- 查看范围:java.io.NotSerializableException: javax.faces.component.html.HtmlInputText
这篇关于绑定属性导致在视图中发现重复的组件 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!