开发者

Initialize Session bean from request scop bean

Hi All

I am using JSF 2.0 , I have two beans UserBean ( Request Scope ) and UserInfoBean( Session Scope ). I want to know how can initialize UserInfoBean from one method of UserBean( Request Scope ) so that i can access it from anywhere.

I also want to know performance wise which is b开发者_StackOverflow社区etter 1. Put a bean as session bean 2. put different different variable in session individually.

Thanks A Lot


Inject the session scoped bean as @ManagedProperty of the request scoped bean. This way it's available to all action methods in the request scoped bean.

E.g.

@ManagedBean
@SessionScoped
public class UserManager {

    private User user;

    // ...
}

and

@ManagedBean
@RequestScoped
public class Login {

    private String username;
    private String password;

    @EJB
    private UserService userService;

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

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

        if (user != null) {
            userManager.setUser(user);
            return "home";
        } else {
            addMessage("Unknown login, try again");
            return null;
        }
    }

    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜