JSF-获取SessionScoped Bean实例 [英] JSF - Get the SessionScoped Bean instance

查看:84
本文介绍了JSF-获取SessionScoped Bean实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序上有此配置. 2个豆:

I have this configuration on my web application. 2 beans :

1°Bean-它检查登录信息;

1° Bean - It checks the login;

@ManagedBean(name="login")
@SessionScoped
public class Login {
    private String nickname;
    private String password;
    private boolean isLogged;

    public String getNickname() { return nickname; }
    public void setNickname(String newValue) { nickname=newValue; }

    public String getPassword() { return password; }
    public void setPassword(String newValue) { password=newValue; }

    public void checkLogin() {
        ... i check on db the nickname and the password ...

        if(USER EXIST) {
            isLogged=true;
        } else {
            isLogged=false;
        }

        return true;
    }
}

2°Bean-管理用户"参数:

2° Bean - Manage User parameter :

@ManagedBean(name="user")
@SessionScoped
public class User {
    private String name;
    private String surname;
    private String mail;

    public User() {
        String[] record=null;
        Database mydb=Configuration.getDatabase();
        mydb.connetti();
        ArrayList<String[]> db_result=null;
        db_result=mydb.selectQuery("SELECT name, surname, mail, domicilio FROM users WHERE nickname='???????'");

        int i = 0;
        while (i<db_result.size() ) {
           record=(String[]) db_result.get(i);
           i++;
        }
    }

    ... getter and setter methods...
}

如您所见,我想知道如何在我的login bean上获得先前设置的昵称,因此我可以在数据库上进行查询.

As you can see, I would like to know how get the nickname setted previously on my login bean, so i can do the query on my DB.

实际上,我需要获取当前会话Bean登录的实例:如何获取它?我应该使用类似session.getBean("login")的东西:)

In fact i need to get the instance of the current-session bean login : how can I get it? I should use somethings like session.getBean("login") :)

希望这个问题很清楚:)

Hope this question is clear :)

推荐答案

使用 @ManagedProperty 注入它并使用

也就是说,这不是正确的方法.您想反过来做.在Login中通过@ManagedProperty(value="#{user}")注入User并在提交操作方法期间执行作业.

That said, this is not the correct approach. You'd like to do it the other way round. Inject User in Login by @ManagedProperty(value="#{user}") and do the job during submit action method.

您还希望将密码放入WHERE子句中.绝对没有必要将整个用户表拖到Java的内存中并一一确定.只需让数据库执行该工作,然后检查它是否返回零行或一行即可.

You'd also like to put the password in WHERE clause as well. There's absolutely no need to haul the entire users table into Java's memory and determine it one by one. Just let the DB do the job and check if it returns zero or one row.

这篇关于JSF-获取SessionScoped Bean实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆