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?
My method depends on the annotation
@RequestScoped
?– ptkato
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.
– irobson
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.– ptkato
I can use Externalcontext within a @Postconstruct method?
– ptkato
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.
– irobson