gpt4 book ai didi

jsf - 重复的 ID。日本科学基金会

转载 作者:行者123 更新时间:2023-12-01 16:21:45 25 4
gpt4 key购买 nike

我在使用 JSF 时遇到问题。谁能告诉我为什么这不起作用?

<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,但是如果我需要的话我该怎么办?

最佳答案

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

我看到三种解决方案可以解决您的问题:

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

第二种解决方案:正如 Wayne Young 所解释的,您可以使用 NamingContainer,它会使用 NamingContainer 的 ID 作为其 ID 的前缀。

第三种解决方案:仅使用一个 <h:selectOneListbox/>然后在Java代码中进行修改。

<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;
}
...
}

关于jsf - 重复的 ID。日本科学基金会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1428317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com