Problems with using JSTL

Asked

Viewed 115 times

4

I’m implementing a WEB application with J2EE, but I’m not able to list the data coming from Servletlistarcursos to the Listdecursos.jsp page that uses JSTL.

  public class ServletListarCursos extends HttpServlet {  

    private static final long serialVersionUID=1L;  
    private static final String CONTENT_TYPE="text/html";  

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws HTTPException, IOException{  

        response.setContentType(CONTENT_TYPE);  
        HttpSession session = request.getSession(true);  
        FachadaControladorDAO fachada = FachadaControladorDAO.getInstancia();  

        Curso curso = new Curso();  
        try {  
          List colecao =  fachada.getListarCursos(curso);  
          session.setAttribute("colecao", colecao);  

          RequestDispatcher rd = request.getRequestDispatcher("view/curso/ListaDeCursos.jsp");  
          rd.forward(request, response);  

        } catch (RepositorioException ex) {  
            Logger.getLogger(ServletListarCursos.class.getName()).log(Level.SEVERE, null, ex);  
        }catch(ServletException ex){  
            ex.printStackTrace();  
        }  

    }         

}  

Servletlistarcursos send the course list("collection") to Listadecursos.jsp.

<%@page contentType="text/html" pageEncoding="UTF-8"%>  
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>Lista de cursos</title>  
         <select id="selecaoCursos" name="selecaoCursos"  >  
            <option value="0" >Selecione o curso</option>  
            <c:forEach items="${colecao}" var="cursos"  >  
               <option  value="${cursos.codigoCurso}"
                        id="codigoCurso">
                        ${cursos.nomeCurso}
               </option>  
            </c:forEach>  
         </select>  
    </body>  
</html>  

Doubt I’m not able to list the values of DB through Servletlistarcursos, using JSTL.

  • I see nothing wrong. is sure that your collection list is filling something?

1 answer

4

Access the list in the correct scope, in your case the object was sent to the session:

session.setAttribute("colecao", colecao); 

In jsp call it that way:

 <c:forEach items="${sessionScope.colecao}" var="cursos"  >  

Or you can use the request scope

request.setAttribute("colecao", colecao); 

jsp

<c:forEach items="${colecao}" var="cursos"  > 
  • 1

    In fact, when the scope in the expression is not explained, the API searches in several scopes in a predefined order. Of course it would be good practice to define the scope to avoid surprises, and always use the most limited scope possible, in this case the requestScope. The search order can be seen here: http://stackoverflow.com/a/875423/1683070

  • I tested the presented shapes, but it hasn’t worked yet. I imported it into the lib folder: jstl-1.1.2.jar and standard-1.1.2.jar, but it’s still not working.

Browser other questions tagged

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