2

I have an object x, which can have one Parameter. A Parameter is either a ParaA or a ParaB.

public abstract class Parameter {

    private final String displayPage;

    public Parameter(String displayPage) {
        this.displayPage= displayPage;
    }
}

public class ParaA extends Parameter {
    private List<String> strings;
    private String currentString;
}

public class ParaB extends Parameter {
    private int min;
    private int max;
}

I want to display each type of Parameter in a different way, so I use <ui:include src="#{bean.parameter.displayPage}" /> to determine the .xhtml of each type of Parameter.

For example the paraA_displayPage.xhtml looks like this:

    <h:outputText value="ParaA" />
    <p:dataTable var="s" value="#{bean.parameter.strings}">
            <p:column>
                    <h:outputText value="#{s}" />
            </p:column>
    </p:dataTable>

This obviously doesn't work, because I have to cast Parameter to ParaA. So how can I manage this casting stuff in jsf?

I could use something ugly like this, as recommended here: https://community.oracle.com/thread/1731149?start=0&tstart=0 but there is probably a better way.

public abstract class Parameter {
    public ParaA getParaA() {
        return (ParaA) this;
    }
    public ParaB getParaB() {
        return (ParaB) this;
    }
}
Paul White
  • 53
  • 1
  • 4
  • to check the type .Could it work in a EL expression? – ccheneson Mar 18 '16 at 09:10
  • Paul, *"This obviously doesn't work, because I have to cast Parameter to ParaA."* that's your own assumption. And it's incorrect. EL has no notion of strong typing. – BalusC Mar 18 '16 at 09:10
  • "This obviously doesn't work, because I have to cast Parameter to ParaA" - are you sure? JSF uses reflection so it should work if the objects are correctly used. – Smutje Mar 18 '16 at 09:11
  • @Smutje: that's EL, not JSF. – BalusC Mar 18 '16 at 09:14
  • 1
    Paul, I guess you concretely got a `PropertyNotFoundException` when you said "obviously doesn't work". Is this true? In that case, is this helpful? http://stackoverflow.com/questions/22613193/javax-el-propertynotfoundexception-when-submitting-uirepeat-with-conditionally/ In a future question better tell the facts not the assumptions :) – BalusC Mar 18 '16 at 09:16
  • You guys are right, I don't have to cast. There was an other problem in my code, but I certainly thought the problem was caused because I didn't cast the object. Thanks for pointing this out. I still have a lot to learn. – Paul White Mar 18 '16 at 10:18

0 Answers0