JSF - Get the SessionScoped Bean instance

前端 未结 2 828
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 10:51

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

1° Bean - It checks the login;

@ManagedBean(name=\"login\")
@SessionScoped
public class Lo         


        
相关标签:
2条回答
  • 2020-12-30 11:27

    Use @ManagedProperty to inject it and use @PostConstruct to access it after bean's construction (because in a normal constructor it would be still null).

    @ManagedBean
    @SessionScoped
    public class User {
    
        @ManagedProperty(value="#{login}")
        private Login login; 
    
        @PostConstruct
        public void init() {
            // Put original constructor code here.
        }
    
        // Add/generate getters/setters and other boilerplate.
    }
    

    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.

    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.

    0 讨论(0)
  • 2020-12-30 11:43

    Also try using the following code:

        ExternalContext tmpEC;
        Map sMap;
        tmpEC = FacesContext.getCurrentInstance().getExternalContext();
        sMap = tmpEC.getSessionMap();
        login loginBean = (login) sMap.get("login");
    
    0 讨论(0)
提交回复
热议问题