随笔-112  评论-73  文章-0  trackbacks-0

在JSF组件开发过程遇到一个问题.就是当自己开发的组件再次提交的时候会提示一个错误:

Duplicate component ID ... found in view.

意思是说在ViewRoot中已经存在一个同名的组件了.这是JSF不允许的.

后来在网上苦苦的找....终于找到这个一篇:http://www.jroller.com/mert/entry/duplicate_component_id_found_in 部分原文如下:


I stated some possible solutions for the exception.

1. If you are dynamically adding child components to a UIComponent, also set the id of the child components explicitly.

2. If you are using JSTL tags like forEach, if, choose and when, make sure you specify unique ids for the components defined between these tags at each iteration. For example,

<c:forEach>
<theComponent id="componentId + unique value for each iteration" />
</c:forEach>

The id for theComponent should be composed with a value that is unique each iteration.

3. If you are developing a custom JSF Component just define id attribute in .tld file. You don?t need to specify id property in tag class. Because it inherits the id from UIComponentBase. So you should also set the properties of the super class within setProperties() method of the component.

4. You cannot determine an id like "_idX" manually in your jsp source code, it is not a valid component identifier (cannot start with "_" ) so re-check the previous steps :)

其中第三条正是我遇到的问题的原因的答案.就是在自己开发的组件中,只需在tld文件中指定id属性,不需在tag.class中再设置id属性.具体的是不能再有setId(String id)这样的方法了.因为父类UIComponentELTag中已存存在这个属性.而且是在tld中一定要设置成静态的String类型.不能是ValueExpression类型

在tld文件中这样设置id属性

<attribute>
<name>id</name>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
</attribute>
然后把tag class中的id 属性和setId方法去掉.

^_^

一点感想:

成功和失败真的是一步的距离、一时的闪念。在寻找答案的过程中真是想放弃自己开发的这个组件,但另一种信念使我没有放弃,最后终于解决了。

用一个小小组件中的一个问题的解决来说成功与失败也许是小题大做,但这个成功的信念是一样的。只要坚持就会有结果!


Technorati :

posted on 2007-08-18 09:13 Libo 阅读(1992) 评论(1)  编辑  收藏

评论:
# re: JSF组件ID重复的问题 2007-08-18 13:47 | Alex
自己开发组建? 精神可嘉,不过个人认为没有太大的实际价值  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: