Sven
Sven

Reputation: 6338

JSF2.0: ManagedProperty Lifecycle?

I have a problem I don't understand: Behind any View I have a controller ManagedBean that is RequestScoped and a data ManagedBean, that holds the data for the view and is SessionScoped.

So there are two views, which are login with loginData and loginController and overview with overviewData and overviewController.

The functionality should be like that:

So, the point is that I want to fill overviewData out of loginController, right after login! (???or if possible right befor overview view is constructed, if possible???).

I tried it with managedProperties, but the one I initiate in loginController is a different object than the managedProperty in overviewController, although they have the same name! How is that possible.

Oh boy, I doubt you guys understand what I mean, so I need to post some code:

LoginController.java

...    
    @ManagedBean
    @RequestScoped
    public class LoginController {

    @ManagedProperty(value = "#{overviewData}")
    private OverviewData overviewData;
    OverviewController overviewController;

    public LoginController(){
        overviewController = new OverviewController ();
    }

    String login() throws Exception {
      UsernamePasswordToken token = new UsernamePasswordToken(loginData.getName(), loginData.getPw().trim());
      try {
      currentUser.login(token);
      overviewController.fillProjects();
...

OverviewController.java

...    
    @ManagedBean
    @RequestScoped
    public class OverviewController {

    @ManagedProperty(value = "#{overviewData}")
    private OverviewData overviewData;

    public void fillProjects(){
      if(overviewData == null){
        overviewData = new OverviewData();
      }
      overviewData.setProjects(projectService.getProjects()); //retrieves data from business logic
    }
...

OverviewData.java

...
    @ManagedBean(name = "overviewData")
    @SessionScoped
    public class OverviewData {

    private List<ProjectDTO> projects;  //that's what the view needs to display the overview

    public void setProjects(List<ProjectDTO> projects) {
      this.projects = projects;
    }
...

I hope that helps to show my problem, if you don't understand it, pls ask in a comment..

Would be nice if you can help me :-)

Cheers...

Upvotes: 1

Views: 3052

Answers (1)

BalusC
BalusC

Reputation: 1109635

You're creating beans yourself using new instead of letting JSF do the job.

overviewController = new OverviewController ();

and

overviewData = new OverviewData();

This is wrong. JSF won't utilize any beans which you've created yourself this way. Remove those lines and add another @ManagedProperty on overviewController inside LoginController (and make the property private).

@ManagedProperty(value="#{overviewController}")
private OverviewController overviewController;

JSF will create the bean itself and set it as managed property directly after parent bean's construction. You just have to access it the usual Java way (without the need for nullchecks).

Upvotes: 2

Related Questions