JSF 2.0 管理用户
我是从 Rails 和 PHP 转向 Java 的,所以我的想法可能有问题。 我想实现一个类似于我在其他系统中所做的登录系统(我知道您可以使用 Java EE 的内置属性来管理登录等,我只是想了解如何手动执行此操作并在之间共享对象beans。)
使用 Rails 或 php,可以轻松维护保存登录用户 ID 的会话变量。然后,PHP 中的全局变量或 Rails 中 applicationController 上的属性可以与用户对象一起预加载,使其在所有控制器/页面中可用。这样,所有页面/控制器都可以轻松访问用户的登录状态和其他参数。
我不知道如何使用 JSF 做类似的事情。
我知道如何使用 JPA 构建用户对象,然后创建一个具有会话范围的托管 bean,该 bean 在使用登录表单中的 loggin 方法登录后保存对用户对象的引用。我理解的那部分:
@ManagedBean
@SessionScope
public class LogginController {
// inject persistence context, or use EJB to do actual loading etc
private User currentUser; //JPA Entity
void loginAction() {
// action from login form
// authenticates user and loads user object into currentUser property
我遇到的问题是,虽然包含 currentUser 属性的托管 bean 具有会话范围,但我如何在其他托管 bean 中访问该属性来获取当前用户?托管 Bean 似乎彼此独立存在。
下面的代码可以接受吗?
@ManagedBean
public class SomeOtherBean {
@ManagedProperty
LoginController loginController;
public void someOtherMethod() {
User myUser = loginController.getCurrentUser();
//
// etc
注入了 bean 的类也必须有 SessionScope 吗? 这是错误的方法吗,我是否需要使用底层 ServletContext 手动设置会话,以便可以在其他 bean 中访问用户(即将登录用户的 id 保存在会话中,然后在访问用户后重新加载用户)不同bean中的id会话变量)?
我这一切都错了吗?我缺少更简单的方法吗?
I am coming to Java from Rails and PHP, so my thinking here may be tainted.
I would like to implement a login system similar to ones I have done in my other systems (I understand you can manage login etc using the built in properties of Java EE, I just want to understand how I could do this manually and share objects amongst beans.)
With rails or php it is easy to maintain a session variable that holds the id of a logged in user. Then a global variable in PHP, or a property on the applicationController in rails can be pre-loaded with the user object, enabling it be available in all controllers/pages. That way the logged in status of the user and other parameters are easily accessible to all pages/controllers.
I don't know how to do a similar thing using JSF.
I know how to build a user object using JPA, and then create a managed bean with session scope that holds a reference to the user object after logging in using a loggin method from a login form. That part I understand:
@ManagedBean
@SessionScope
public class LogginController {
// inject persistence context, or use EJB to do actual loading etc
private User currentUser; //JPA Entity
void loginAction() {
// action from login form
// authenticates user and loads user object into currentUser property
Where I get stuck, is that although the managed bean that contains the currentUser property has session scope, how would I access that property in other managed beans to get the current user? Managed beans seem to exist in isolation from each other.
Is the following code acceptable?
@ManagedBean
public class SomeOtherBean {
@ManagedProperty
LoginController loginController;
public void someOtherMethod() {
User myUser = loginController.getCurrentUser();
//
// etc
Would the class having the bean injected also have to have SessionScope too?
Is this the wrong approach, would I need to manually set the session using the underlying ServletContext so that the user was accessible in other beans (i.e. save the id of the logged in user in the session, and then reload the user after accessing the user id session variable in different beans)?
Am I going about this all wrong? Is there an easier way I am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的(假设它是伪的;在实际代码中它应该是一个
private
属性,并且您应该像这样指定托管属性值@ ManagedProperty("#{loginController}")
并至少提供一个 setter 方法)。不一定。它完全可以是请求或视图范围的 bean,但不是应用程序范围的 bean(这会导致异常)。至少,您应该非常小心地选择 bean 范围。不要为其保存的数据选择太宽的范围。另请参阅如何选择正确的 bean 范围?。
Yes (assuming that it's pseudo; in real code it should be a
private
property and you should specify the managed property value like so@ManagedProperty("#{loginController}")
and provide at least a setter method).Not necessarily. It can perfectly be a request or view scoped bean, but not an application scoped bean (it would result in an exception). At least, you should be very careful with choosing the bean scope. Do not choose a too broad scope for the data it holds. See also How to choose the right bean scope?.