How to handle polymorphism with JSF2?

前端 未结 2 1899
闹比i
闹比i 2021-01-06 02:41

I need to display/edit polymorphic entities.

My abstract class is Person. My concrete classes are PhysicalPerson and MoralP

相关标签:
2条回答
  • 2021-01-06 03:07

    There is no such thing as instanceof in EL. You can however (ab)use Object#getClass() and access the getters of Class in EL as well. Then just determine the outcome in the component's rendered attribute.

    <h:panelGroup rendered="#{entity.class.name == 'com.example.PhysicalPerson'}">
        <p>According to Class#getName(), this is a PhysicalPerson.</p>
    </h:panelGroup>
    <h:panelGroup rendered="#{entity.class.simpleName == 'MoralPerson'}">
        <p>According to Class#getSimpleName(), this is a MoralPerson.</p>
    </h:panelGroup>
    

    A custom EL function would be more clean however. Note that the above doesn't work on Tomcat 7 and clones due to extremely restrictive restrictions of allowed propertynames in EL. Java reserved literals such as class are not allowed anymore. You'd need #{entity['class'].name} and so on instead.

    0 讨论(0)
  • 2021-01-06 03:20

    Another way is to create an abstract method in a base class, which will return you some mark of what instance you have, and implement it in your subclasses, like this:

    public abstract class Person {
    
    public abstract boolean isPhysical();
    
    }
    
    public PhysicalPerson extends Person {
    
    public boolean isPhysical() {
         return true;
    }
    
    }
    

    and then in jsf:

    <h:panelGroup rendered="#{entity.physical}">
        <p>this is a PhysicalPerson.</p>
    </h:panelGroup>
    <h:panelGroup rendered="#{ not entity.physical}">
        <p>this is a Moral Person.</p>
    </h:panelGroup>
    

    However the class checking approach is more universal.

    0 讨论(0)
提交回复
热议问题