How to pass as a parameter in an item method of a foreach, a URL variable?

Asked

Viewed 430 times

0

I am developing a project in Java, academic work. My question is in the following. I can display items related to a logged in user, where the value(nick) is set in a session with login name. Behold:

<jsp:useBean id="dao" class="dao.CestaDAO" />
<c:forEach var="item" items="${dao.findItens(login)}">
  ${item.nome}
</c:forEach>

Everything works perfectly. However, I have a problem when as an administrator, by clicking on a link that passes a variable via URL(?clienteLogin=so-and-so). I cannot take the possible value and play as parameter, in findItens(). I must be making a silly mistake, Java is not the language I master. Thank you!

String cliente=request.getParameter("clienteLogin").toString();

<jsp:useBean id="dao" class="dao.CestaDAO" />
<c:forEach var="item" items="${dao.findItens(cliente)}">
  ${item.nome}
</c:forEach>

1 answer

0


Well, I was able to solve it using sessionScope. Only this way was it possible to set the attribute(client) and use it as parameter in ${dao.findItens(client)}.

         <c:choose>
            <c:when test="${sessionScope.login != null && sessionScope.login ne 'admin'}">
                <% pageContext.setAttribute("cliente", String.valueOf(session.getAttribute("login")), PageContext.SESSION_SCOPE);
                %>
            </c:when>

            <c:when test="${sessionScope.login == 'admin'}">

                <% pageContext.setAttribute("cliente", String.valueOf(request.getParameter("clienteLogin")), PageContext.SESSION_SCOPE);
                %>
            </c:when>
        </c:choose>

        <h1>Lista de  ${sessionScope.cliente}</h1>

        <jsp:useBean id="dao" class="dao.CestaDAO" />
        <ul>
            <c:forEach var="item" items="${dao.findItens(cliente)}">
                <li>${item.nome}</li>
            </c:forEach>
        </ul> 

Browser other questions tagged

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