注入會話範圍的bean作爲請求的@ManagedProperty
作用域bean。這樣它可以在請求作用域bean中的所有操作方法中使用。
例如
@ManagedBean
@SessionScoped
public class UserManager {
private User user;
// ...
}
和
@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;
}
}
// ...
}