在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 : JSF
posted on 2007-08-18 09:13
Libo 閱讀(2001)
評論(1) 編輯 收藏