JSF组件ID重复的问题

在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. <o:p>

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

<o:p>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,

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

<o:p>The id for theComponent should be composed with a value that is unique each iteration.</o:p>

<o:p>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.

<o:p>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

<think>好的,我现在需要解决用户关于JSF中更新片段内ID问题。首先,我得回忆一下JSF的基本知识,特别是部分更新和片段处理的部分。用户提到的是“更新片段内ID”,这可能涉及到AJAX更新特定的组件或者动态更改ID本身。我记得在JSF中,通常使用<f:ajax>标签或者h:commandButton等组件的ajax功能来部分更新页面区域。这部分更新是通过指定render属性中的组件ID来实现的。但是,如果片段中的组件ID是动态生成的,或者需要根据某些条件改变,可能需要用到相对ID或使用命名容器来确保正确的客户端ID生成。接下来,我需要确认用户的问题具体是指更新某个片段的ID属性,还是通过AJAX来更新该片段内的内容。可能是后者,即如何正确指定要渲染的片段ID,以便AJAX请求后只更新该部分。这时候,可能需要考虑使用客户端ID,尤其是在复合组件重复组件中,因为ID可能会被JSF自动附加前缀。另外,JSF2.0引入了部分状态保存和部分视图渲染,可能相关的标签如<f:ajax>和<f:param>。例如,在commandButton中使用render属性指定要更新的区域,该区域可以是某个组件ID,或者使用相对ID如":formId:componentId"来避免前缀问题。 还需要考虑PrimeFaces或者其他第三方库中的方法,比如p:ajax或p:commandButton,它们可能有更简便的方式处理片段更新。例如,PrimeFaces的update属性支持@parent或@form这样的关键字来定位组件。可能的问题点包括客户端ID的动态生成、命名容器的影响(如h:form、ui:repeat)、以及如何正确引用这些ID在AJAX请求中。如果用户动态生成ID,可能需要使用EL表达式或者在后台Bean中预先生成正确的客户端ID。最后,我需要整理这些思路,分步骤说明,并给出示例代码,确保用户能够理解如何实现。同时,生成的相关问题需要围绕JSF的部分更新、ID管理、常见问题等展开,帮助用户进一步学习。</think>在JSF中更新特定片段内的组件ID或内容,主要通过以下方法实现: ### 一、使用原生JSF的AJAX更新 1. 通过`<f:ajax>`标签指定`render`属性: ```xhtml <h:commandButton value="更新"> <f:ajax render="targetComponentId"/> </h:commandButton> ``` 2. 使用`execute`和`render`属性组合: ```xhtml <h:commandButton value="提交并更新" action="#{bean.updateData}"> <f:ajax execute="@form" render=":targetForm:resultPanel"/> </h:commandButton> ``` 注意客户端ID需要包含命名容器前缀(如`:formId:`)[^1] ### 二、动态ID处理 当处理重复组件时: ```xhtml <ui:repeat value="#{bean.items}" var="item"> <h:outputText id="item_#{item.id}" value="#{item.name}"/> <h:commandButton value="更新"> <f:ajax render="item_#{item.id}"/> </h:commandButton> </ui:repeat> ``` 需确保生成的客户端ID在视图范围内唯一[^2] ### 三、PrimeFaces优化方案 使用PrimeFaces的组件选择器: ```xhtml <p:commandButton update="@(.resultClass)" action="#{bean.refresh}"/> ``` 通过CSS类名定位要更新的组件 ### 四、条件更新实现 ```xhtml <h:commandButton value="条件更新"> <f:ajax render="#{bean.updateCondition ? 'componentA' : 'componentB'}"/> </h:commandButton> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值