【发布时间】:2013-12-12 05:34:39
【问题描述】:
@SessionScoped public class User {
... //settings, attributes, etc
}
@ViewScoped public class FooController {
@ManagedProperty(value="#{user}")
private User user;
...
}
@RequestScoped public class LoginController {
@ManagedProperty(value="#{user}")
private User user;
public String login() {
//handle Servlet 3.0 based authenticate()... if success, make new User object and slap it into context
request.getSession().setAttribute("user", user);
return "?faces-redirect=true";
}
...
}
xhtml 页面几乎在每个页面上都包含登录控件。理想的情况是他们能够登录,页面将刷新,现有的FooController 将引用当前登录的用户,该用户有条件地呈现按钮/元素。行为是发生了登录,但FooController 视图仍然是“有效的”,因此不再尝试再次注入托管 bean。如果我离开页面,然后返回到它[重建视图范围的 bean],用户 bean 会很好地重新注入......但我不希望有那个中间步骤。有什么想法吗?
我尝试了各种形式的FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("user");,希望它能从会话中重新拉出它,但无济于事。我不想在我的 LoginController 中紧密耦合代码来引用专门使 FooController 或 BarController 或任何其他引用用户 bean 的对象无效。
【问题讨论】:
标签: jsf jsf-2 view-scope managed-property