0

我有一个会话范围的 bean 来管理我的用户登录和登录过滤器,但我不确定如何在另一个 bean 中获取用户对象以根据登录的用户显示某些数据。

这是我的登录 Bean:

 @ManagedBean(name="user")
        @SessionScoped
        public class LoginBean {

            private String username;
            private String password;
            private UsuariosEntity current;

         public LoginBean(){}
        @EJB
        UsuariosDAO userService=new UsuariosDAO();



            public String login() {

           try{
           current = userService.buscar(username, password);

           System.out.println(current.getUsuario());
           }catch(Exception e){

           }
                if (current == null) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unknown login, try again"));
                    return (username = password = null);
                } else {
                    return "/Contenido/vistaProcesos?faces-redirect=true";
                }
            }

            public String logout() {
                System.out.println("logout");
                FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
                return "/Login?faces-redirect=true";
            }

            public boolean isLoggedIn() {
                return current != null;
            }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public UsuariosEntity getCurrent() {
            return current;
        }



    }

登录过滤器:

@WebFilter("/Contenido/*")
public class FiltroLogin implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        LoginBean user = (LoginBean) req.getSession().getAttribute("user");

        if (user != null && user.isLoggedIn()) {
            // User is logged in, so just continue request.
            chain.doFilter(request, response);
        } else {
            // User is not logged in, so redirect to index.
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect(req.getContextPath() + "/Login.jsf");
        }
    }

    @Override
    public void destroy() {

    }


}

这就是我想要做的:

@ManagedBean(name = "procesos")
@ViewScoped
public class ProcesosBean implements Serializable {
    private List<VistaProcesosEntity> procesos;

    @PostConstruct
    public void init(){

          //i want to fill procesos List with data, depending of the user who is logged on.

       procesos=new ArrayList<VistaProcesosEntity>();

      /* LoginBean user; // i dont know from where should i get the user object.. 

       if (user.getCurrent().getTipo()=="Planta") {
        procesos= Procesos.getALL();
        }else if(user.getCurrent().getTipo()=="Exportadora"){
            procesos=Procesos.getALLbyExportadora(user.getCurrent().getUsuario());
        }  else if (user.getCurrent().getTipo()=="Productor"){
            procesos=Procesos.getALLbyProductor(user.getCurrent().getUsuario());
        } else{
           procesos= Procesos.getALL();
       }   */
    }

    public List<VistaProcesosEntity> getProcesos() {
        return procesos;
    }
4

1 回答 1

1
@ManagedBean(name = "procesos")
@ViewScoped
public class ProcesosBean implements Serializable {
    @ManagedProperty(value="#{user}") // this references the @ManagedBean named user
    private LoginBean loginBean;

    private List<VistaProcesosEntity> procesos;

    @PostConstruct
    public void init(){
        UsariosEntity current = loginBean.getCurrent();
    }
}

我已经扩展了你ProcesosBean的一点。注意函数中的引用LoginBean和检索。如果您还有任何问题或不清楚(足够),请询问。UsariosEntityinit()

于 2013-04-30T16:24:50.340 回答