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?