Taglib - Condition test between JSP and MODEL

Asked

Viewed 241 times

0

I am trying to make my JSP (index.jsp) execute a condition (if) with taglib, to decide which menu it will import, depending on an attribute (Boolean)) that is in the model (user.java).

I have already performed the debug to check if the +validate method is pulling the correct information from the database. It is working normal.

But the problem is passing it on to JSP, so she can take the test.

The test I’m trying to do is simple:

<c:if test ="${usuario.permissao == true}">
    <c:import url="MenuAdmin.jsp"/>
</c:if>

This is my command code, where I perform login validation. (I don’t know if I am passing correctly to JSP.)

public class FazerLogin implements Command {
    @Override
    public void executar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    String login = request.getParameter("username");
    String senha = request.getParameter("password");

    Usuario usuario = new Usuario();
    usuario.setUsuario(login);
    usuario.setSenha(senha);
    UsuarioService service = new UsuarioService();

    if(service.validar(usuario)){
        HttpSession session = request.getSession();
        session.setAttribute("usuarioLogado", usuario.getUsuario());
        System.out.println("Logou "+ usuario.getUsuario());
    } else {
        System.out.println("Não logou "+ usuario.getUsuario());
        throw new ServletException("Usuario/Senha inválidos");
    }
    request.setAttribute("usuario", usuario);
    response.sendRedirect("Index.jsp");
}}

When I do the test to check if {user.permissao == false}, it returns true. But strange that the method in debug, shows that the permission is true.

  • when you do request.setAttribute("usuario", usuario); you didn’t want to do request.setAttribute("usuario", usuario.getUsuario()); ?

  • But the user is already

  • So, this new user only has login and password (at least that’s what you could see in this code) where you are putting the permission ?

  • Inside the validate method

  • Try { Connection Conn = Connectionfactory.obtemConexation(); Try (Preparedstatement stm = Conn.prepareStatement(sqlSelect);) { stm.setString(1, usuario.getUsuario(); stm.setString(2, usuario.getSenha(); stm.execute(); Try (Resultset rs = stm.executeQuery();).

1 answer

0

I was able to solve the issue of JSP understanding the parameter.

I changed the validation of:

request.setAttribute para session.setAttribute

Now the problem is changing menu. Because when the user logs in, "index.html" imports the "menu.jsp". Inside the "menu.jsp" has an if.

<c:if test="${usuario.permissao == true}">
reponse.sendRedirect("MenuAdmin.jsp");
</c:if>

But this does not work that redirect. Would this code even ? 'Cause I couldn’t find any other redirect.

Browser other questions tagged

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