Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request scoped property in session scoped JSF bean

I would like to have a session scoped JSF bean with one property that is request (page) scoped. Is it possible at all?

like image 370
Jarek Avatar asked May 26 '11 09:05

Jarek


People also ask

What is session scoped in JSF?

The session scope allows you to create and bind objects to a session. It gets created upon the first HTTP request involving this bean in the session and gets destroyed when the HTTP session is invalidated. The request scope is present in JSF and CDI and functions in the same way.

How do you decide what should be the scope of the bean?

If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage. ApplicationScope: The application scope persists for the entire duration of the web application.

What is @SessionScoped?

The annotation @SessionScoped specifies that a bean is session scoped ie there will be only one instance of the class associated with a particular HTTPSession.

What are the different kinds of bean scopes in JSF?

JavaServer Faces (JSF) In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value). Managed beans works as Model for UI component. Managed Bean can be accessed from JSF page.


1 Answers

No, that's not possible. Managed property injection only happens during creation of the bean. However, when a session scoped bean is been created there is not necessarily a request present and the injected request scoped bean would be invalid in subsequent requests in the remnant of the session.

Do it the other way round. E.g.

@ManagedBean
@SessionScoped
public class UserManager {

    private User current;

    // ...
}

and

@ManagedBean
@RequestScoped
public class Login {

    private String username;
    private String password;

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

    @EJB
    private UserService userService;

    public String submit() {
        User user = userService.find(username, password);

        if (user != null) {
            userManager.setCurrent(user);
            return "home?faces-redirect=true";
        } else {
            addErrorMessage("Unknown login, please try again");
            return null;
        }
    }

    // ...
}
like image 53
BalusC Avatar answered Oct 14 '22 13:10

BalusC