问题描述
我有一个超类Person
:
public class Person {
public abstract Type getType();
}
我有它的 2 个子类:
I have 2 subclasses of it:
public class JuridicalPerson extends Person {
public Type getType() {
return Type.JP;
}
public List<JuridicalBelong> getJuridicalBelongs() {
return juridicalBelongs;
}
}
public class NaturalPerson extends Person {
public Type getType() {
return Type.NP;
}
public List<NaturalBelong> getNaturalBelongs() {
return naturalBelongs;
}
}
JuridicalBelong
和 NaturalBelong
属性不同,不能子类化.
JuridicalBelong
and NaturalBelong
have different properties and can't be subclassed.
我将它们放在 List<Person>
中,我想在 JSF/Facelets 中显示如下:
I have them in a List<Person>
which I'd like to present in JSF/Facelets as follows:
<ui:repeat value="#{bean.persons}" var="person">
<h:panelGroup rendered="#{person.type eq 'JP'}">
<ui:repeat value="#{person.juridicalBelongs}" var="juridicalBelong">
...
</ui:repeat>
</h:panelGroup>
<h:panelGroup rendered="#{person.type eq 'NP'}">
<ui:repeat value="#{person.naturalBelongs}" var="naturalBelong">
...
</ui:repeat>
</h:panelGroup>
</ui:repeat>
但是,这会导致以下异常:
However, this causes the following exception:
javax.el.PropertyNotFoundException:com.example.NaturalPerson"类没有juridicalBelongs"属性.
这怎么可能?根据我的 rendered
条件
How is this possible? As per my rendered
condition
<h:panelGroup rendered="#{person.type eq 'JP'}">
它应该忽略NaturalPerson
,对吧?
推荐答案
这是由 Mojarra 的 <ui:repeat>
状态管理中的错误引起的,当您使用 <ui:repeat>
内的 EditableValueHolder 组件(输入字段)也是如此.这是根据 issue 3219 修复的.Mojarra 2.2.7 并且对于向后移植到 Mojarra 的 JSF 2.0/2.12.1.29 根据 issue 3224.所以至少升级到那个版本(或者只是根据 Mojarra 主页提供的最新版本)应该这样做.
This is caused by a bug in state management of Mojarra's <ui:repeat>
which will expose when you use EditableValueHolder
components (input fields) inside the <ui:repeat>
as well. This is fixed as per issue 3219. The fix is available in Mojarra 2.2.7 and for JSF 2.0/2.1 backported to Mojarra 2.1.29 as per issue 3224. So upgrading to at least that version (or just the latest available as per Mojarra homepage) should do it.
否则,最好的办法是将 <ui:repeat>
替换为 <c:forEach>
.
Otherwise, your best bet is to replace <ui:repeat>
by <c:forEach>
.
这篇关于ui:repeat 中条件渲染子类的 PropertyNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!