PropertyNotFoundException on conditionally rendered subclasses in ui:repeat

▼魔方 西西 提交于 2019-11-27 04:07:41

问题


I have a superclass Person:

public class Person {
    public abstract Type getType();
}

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 and NaturalBelong have different properties and can't be subclassed.

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: The class 'com.example.NaturalPerson' does not have the property 'juridicalBelongs'.

How is this possible? As per my rendered condition

<h:panelGroup rendered="#{person.type eq 'JP'}">

it should ignore NaturalPerson, right?


回答1:


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 3215. 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 3221. So upgrading to at least that version (or just the latest available as per Mojarra homepage) should do it.

Otherwise, your best bet is to replace <ui:repeat> by <c:forEach>.




回答2:


In my case I didn't had the option to upgrade Mojarra's version and to avoid c:forEach (that causes many side-effects when used with ui optionally rendered components) I replaced ui:repeat with a p:dataList and it worked. You'll have to do some CSS styling to hide the bullets but I think it's worth the price. I hope it helps someone ;)



来源:https://stackoverflow.com/questions/25594147/propertynotfoundexception-on-conditionally-rendered-subclasses-in-uirepeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!