1

I have six types of Project objects, each defined by its type (Enum). Each project type has it's own backing bean (Project1Bean, Project2Bean, etc...). After selecting a project from a list I navigate to a page selectedProject.xhtml where I would like to initialize to particular backing bean by the project type. For now I had a single bean which loads the project from the DB and this bean is initialized through the preRenderView event. Then there are a lot of methods in the bean, that always check the project type and perform some actions. I was wondering if there is some better way to initialize the particular project bean by the project type. I was thinking about having a SelectedProjectBean which in the preRenderView method loads the project and initializes the particular project bean. But I don't like this solution very much.

class SelectedProjectBean {
    Project project;

    @Inject
    Project1Bean project1Bean;

    @Inject
    Project2Bean project2Bean;

    public void preRenderView() {

        project = loadProject();

        switch(project.getType()) {
            case Type1:
                project1Bean.preRenderView();
                break;                
            case Type2:
                project2Bean.preRenderView();
                break;
        }
    }

and in the selectedProject.xhtml

<ui:define name="metadata">
    <f:metadata>            
        <f:event type="preRenderView" listener="#{selectedProjectBean.preRenderView}"/>
    </f:metadata>
</ui:define>

Isn't it possible to have an interface for the projectbeans and use some qualifiers and have only one named bean for projects? I.e. let the container decide which bean would it initialize?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • Don't know for sure, but would something like ['cdi qualifiers'](http://stackoverflow.com/questions/15451422/how-to-use-cdi-qualifiers-with-multiple-class-implementations) work? – Kukeltje Mar 06 '15 at 10:06
  • That is what I would like to use, but I think that works only for injection. I couldn't find any way to somehow use qualifiers in JSF views. – Filip Majernik Mar 06 '15 at 10:58
  • cdi and jsf are tightly integrated,and cdi is slowly deprecating the old good jsf managed beans, to use cdi beans, so i do not see the reason why you should not be able to inject your interfaces in the "selectedProjectBean" using cdi qualifiers – maress Mar 06 '15 at 13:11

0 Answers0