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 LotInject 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;
}
}
// ...
}
精彩评论