重复的ID.联合部队 [英] Duplicate Id. JSF

查看:162
本文介绍了重复的ID.联合部队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JSF有问题.谁能说为什么这行不通?

I have a problem with JSF. Can anyone say why this doesn't work?

<h:selectOneListbox
  id="lang" size="5"
  value="#{MbInstitution.node.lang}"
  valueChangeListener="#{MbInstitution.changeLanguage}"
  rendered="#{MbInstitution.view}"
  >
 <a4j:support event="onchange" reRender="shortDesc, fullDesc"/>
 <f:selectItems value="#{MbInstitution.languagesByInstitute}"/>
</h:selectOneListbox>
<h:selectOneListbox
  id="lang" size="5"
  disabled="#{!MbInstitution.managingNew}"
  value="#{MbInstitution.node.lang}"
  rendered="#{!MbInstitution.view}"
  >
 <f:selectItems value="#{MbInstitution.availableLanguages}"/>
</h:selectOneListbox>

它说:组件instForm:lang的ID重复" 我知道我有2个具有相同ID的元素,但是一个元素仅在另一个元素不存在时才呈现.因此,我认为这不会成为问题.其实这根本不是什么大问题,因为我不需要这个ID,但是如果我需要我该怎么办呢?

It says: "duplicate Id for a component instForm:lang" I know that i have 2 elements with same Id, but one is rendered only when another isn't. So, i didn't think it would be a problem. Actually it's not a big problem at all as i don't need this id, but what if i needed then what would i do?

推荐答案

您的问题是,这两个组件是此页面的JSF组件树的一部分.即使不能同时显示它们,它们也共享相同的ID,这是JSF不允许的.

Your problem is that these two components are part of the JSF Component tree for this page. And even if they cannot be displayed at the same time, they share the same ID, which is not allowed by JSF.

我看到了三种解决您的问题的方法:

I see three solutions to solve your problem:

第一个解决方案:定义两个不同的ID

First solution: Define two differents ID

第二个解决方案:您可以按照Wayne Young的说明使用NamingContainer,该ID将以NamingContainer的ID开头.

Second solution: You can, as explained by Wayne Young, use a NamingContainer, which will prefix their ID by the ID of the NamingContainer.

第三种解决方案::仅使用一个<h:selectOneListbox/>,然后在Java代码中有所作为.

Third solution: Use only one <h:selectOneListbox/> and then make the difference in the Java code.

<h:selectOneListbox id="lang" size="5" disabled="#{!MbInstitution.managingNew}" value="#{MbInstitution.node.lang}" valueChangeListener="#{MbInstitution.changeLanguage}">
    <a4j:support event="onchange" reRender="shortDesc, fullDesc" rendered="#{MbInstitution.view}"/>
    <f:selectItems value="#{MbInstitution.languages}"/>
</h:selectOneListbox>

Java代码:

public List<SelectItem> getLanguage() {
    if (isView()) {
        return getLanguagesByInstitute();
    } else {
        return getAvailableLanguages();
    }
}

public void changeLanguage(ValueChangeEvent evt) {
    if (!isView()) {
        return;
    }
    ...
}

这篇关于重复的ID.联合部队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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