How to get the customer's request?

4

I'm trying to get the user session from the request, but I'm not sure if I'm capturing the request correctly:
I have a Managed Bean ( SessionScoped ) with data. The following block creates a user's session in a login situation:

@ManagedBean
@SessionScoped
public class Usuario {
    private String login;
    private String senha;
    private String nome;
    private String email;
    ...
    //getters, setters.
    ...
    public Boolean logon() {
        //validações que checam se o usuário existe e colocou a senha correspondente.
    }
}
<p:inputText value="#{Usuario.login}"/>
<p:inputText value="#{Usuario.senha}"/>
<p:commandButton value="entrar" action="#{Usuario.logon}"/>

The user can have clients and if you want to see who they are, I have unrolled the following:

@ManagedBean
@RequestScoped
public class Cliente {
    private String nome;
    private String email;
    private String endereco;
    ...
    //getters, setters.
    ...
    public ArrayList<Cliente> clientes(HttpServletRequest request) {
        try {
            return new ClienteDAO().selectClientes(request);
        }
        catch (SQLException ex) {
            //
        }
        return null;
    }
}
public class ClienteDAO {
    public ArrayList<Cliente> selectClientes(HttpServletRequest request) throws SQLException {
        ArrayList<Cliente> dataGrid;
        try {
            Usuario usuario = (Usuario)((HttpServletRequest)request).getSession().getAttribute("Usuario");
            //PreparedStatement ps...
            //O comando tem um parâmetro que busca de acordo com o login do usuário.
            ps.setString(1, usuario.getLogin);
            //Executa o comando e atribui os dados do ResultSet ao ArrayList dataGrid;
        }
        return dataGrid;
    }
}
<h:form>
    <p:dataGrid value="#{Cliente.clientes}" var="cliente">
        <p:outputLabel value="#{cliente.getNome}"/>
        <p:outputLabel value="#{cliente.getEmail}"/>
    </p:dataGrid>
</h:form>

Does my method work? Regardless of the answer, are there other ways to do what I want?

    
asked by anonymous 07.11.2014 / 01:32

1 answer

2

Yes, it should work. In addition, you can use a more sophisticated method to work with session objects in JSF, which is to use ExternalContext .

ExternalContext contextoExterno = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> mapaSessao = contextoExterno.getSessionMap();
Usuario usuario = (Usuario) mapaSessao.get("usuario");

Another suggestion would be that you do not mix API-specific classes within your model, in your example, HttpServletRequest is a parameter of your Cliente model object. What you could do is pass the User or your already mounted ID into the parameter. It would also remove the DAO coupling from within the model object and, to close it, would reverse the behavior, causing the Usuario object to have a method that returns its clients and not the Cliente returning a user's clients. But there it is a more architectural issue and it escapes the focus of the question.

    
10.11.2014 / 16:25