Aaron,

If you are after a reference to the LoginController, you could try this:

VariableResolver vr =
FacesContext.getCurrentInstance().getApplication().getVariableResolver();

LoginController lc =
(LoginController)vr.resolveVariable(FacesContext.getCurrentInstance(),
"LoginCtl");

This is simpler than creating a value binding.  It will use the whole
managed bean deal to create the bean if it doesn't exist, etc.

sean


On Wed, 02 Feb 2005 21:29:00 -0600, Aaron Bartell
<[EMAIL PROTECTED]> wrote:
> Hey all you great minds!  I have found a comfortable way to access other
> managed-bean's in a session and I would like to know if it is a good
> implementation.  Here is a LoginController class that act's as both a
> POJO and also allows you to retrieve the same named session object from
> the faces configuration using static methods.  Before using this
> approach I was up in arms on where to put methods like
> LoginController.loggedIn(). I am sure others have done something similar
> to this and I am not the first, but I would still like input on whether
> it is good or bad.
> 
> Thoughts?
> Aaron Bartell
> 
> -----faces-config.xml entry-----
>     <managed-bean>
>         <managed-bean-name>LoginCtl</managed-bean-name>
> 
> <managed-bean-class>com.mowyourlawn.controller.LoginController</managed-bean-class>
>         <managed-bean-scope>session</managed-bean-scope>
>     </managed-bean>
> 
> -----Usage Examples-----
> 
> userid = LoginController.user().getUid();
> ...
> LoginController.loggedIn(Boolean.FALSE);
> ...
> if(!LoginController.loggedIn().booleanValue()){
>  // go to login.jsp
> }
> 
> -----LoginController Class-----
> 
> package com.mowyourlawn.controller;
> 
> import javax.faces.application.FacesMessage;
> import javax.faces.context.FacesContext;
> 
> import com.mowyourlawn.dao.User;
> import com.mowyourlawn.util.Const;
> 
> public class LoginController {
> 
>     private User user = new User();
>     Boolean loggedIn = new Boolean(false);
> 
>     public LoginController() {
>     }
> 
>     public static User user() {
>         FacesContext fc = FacesContext.getCurrentInstance();
>         return (User)
> fc.getApplication().createValueBinding("#{LoginCtl.user}").getValue(fc);
>     }
> 
>     public static Boolean loggedIn() {
>         FacesContext fc = FacesContext.getCurrentInstance();
>         return (Boolean)
> fc.getApplication().createValueBinding("#{LoginCtl.loggedIn}").getValue(fc);
>     }
> 
>     public static void loggedIn(Boolean x) {
>         FacesContext fc = FacesContext.getCurrentInstance();
> 
> fc.getApplication().createValueBinding("#{LoginCtl.loggedIn}").setValue(fc,
> x);
>         return;
>     }
> 
>     public String validLogin() {
>         if (LoginController.user().validLogin()) {
>             LoginController.loggedIn(Boolean.TRUE);
>             return "success";
>         }
>         LoginController.loggedIn(Boolean.FALSE);
>         FacesContext fc = FacesContext.getCurrentInstance();
>         FacesMessage facesMessage = new FacesMessage("You have entered
> an invalid user name and/or password.");
>         fc.addMessage("loginform", facesMessage);
>         return "failure";
>     }
> 
>     public String logout() {
>         LoginController.loggedIn(Boolean.FALSE);
>         FacesContext fc = FacesContext.getCurrentInstance();
>         FacesMessage facesMessage = new FacesMessage("You have been
> logged out of the system.");
>         fc.addMessage("loginForm", facesMessage);
>         return "logout";
>     }
> 
>     public boolean getAdminAccess() {
>         if (LoginController.user().getAccesslevel().intValue() ==
> Const.AL_ADMIN.intValue()
>                 || LoginController.user().getAccesslevel().intValue() ==
> Const.AL_OPERATOR.intValue()) return true;
>         return false;
>     }
> 
>     public User getUser() {
>         return user;
>     }
> 
>     public void setUser(User usr) {
>         this.user = usr;
>     }
> 
>     public Boolean getLoggedIn() {
>         return loggedIn;
>     }
> 
>     public void setLoggedIn(Boolean loggedIn) {
>         this.loggedIn = loggedIn;
>     }
> }
> 
>

Reply via email to