0
votes

Using JSF 2.0 and Spring, I use an @RequestScope managed bean. This bean stores information about the logged-in user. It loads the user from the DB in a @PostConstruct method:

@PostConstruct
public void init() {
    String username = login.getUsername();
    user = userDao.load(username);
}

The logged-in user can then trigger on action on the page that updates the user in database (using another managed bean).

However, the @RequestScope bean is constructed at the beginning of the request, which is before the call to the updating action. As a result, when the page is redisplayed, the User variable still has its old values.

My question is: do I have a way to run my loading method not at the beginning of the request, but after the request has been sent? Or am I dealing with this in the wrong way?

Thanks for your insight,
Sébastien

1

1 Answers

1
votes

The logged-in user can then trigger on action on the page that updates the user in database (using another managed bean).

The same managed bean should have been updated at that point. If you can't reuse the same managed bean for some reason, then you should manually do it by accessing it in the action method and calling the setters yourself.


Update: based on the comments, here's how the beans should be declared and injected and used in your particular requirement:

@ManagedBean(name="#{login}")
@SessionScoped
public class LoginManager {

    private String username;

    // ...
}

@ManagedBean(name="#{user}")
@RequestScoped
public class UserManager {

    @ManagedProperty(value="#{login}")
    private LoginManager login;
    private User current;

    @PostConstruct
    public void init() {
        current = userDAO.find(login.getUsername());
    }

    // ...
}

@ManagedBean(name="#{profile}")
@RequestScoped
public class ProfileManager {

    @ManagedProperty(value="#{user}")
    private UserManager user;

    public void save() {
        userDAO.save(user.getCurrent());
    }

    // ...
}

<h:form>
    <h:inputText value="#{user.current.firstname}" />
    <h:inputText value="#{user.current.lastname}" />
    <h:inputText value="#{user.current.birthdate}" />
    ...
    <h:commandButton value="Save" action="#{profile.save}" />
</h:form>