How to get the client’s requisition?

Asked

Viewed 630 times

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 may have customers and if you wish 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, there are other ways to do what I want?

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 the use of Externalcontext.

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

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

  • My method depends on the annotation @RequestScoped?

  • This just defines the scope of your class. In this case, the Client class being Requestscoped will be recreated for each new request not maintaining their state. Different from Sessionscoped, which will continue to live as long as the user session is active.

  • I need to explain in the method call the attribute (Cliente.clientes(request))? Because when I leave no parameter, Netbeans accuses that the "clients" method is unknown.

  • I can use Externalcontext within a @Postconstruct method?

  • Hi @Patrick, to help organize the questions on the site, please create a new topic per question. Despite being related, your question is no longer the same as the topic, it has even been resolved. A hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.