JSP - 没有从Servlet

时间:2018-04-16 16:20:01

标签: java html jsp

我已经搜索了很多解决方案,但没有任何效果。 我有一个生成List的Servlet,我将它发送到.jsp,但我无法检索List。我尝试过设置为sessionScope和requestScope,但仍然无法正常工作。我从Servlet访问页面,我在控制台上打印List项目(System.out.prinltn),项目就在那里。

我的Servlet代码:

@WebServlet(name = "ValidaLoginHotelServlet", urlPatterns = {"/ValidaLoginHotelServlet"})
public class ValidaLoginHotelServlet extends HttpServlet {
    @Resource(name="jdbc/SistemaHotelLocal")
    DataSource dataSource;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        LoginHotelFormBean lhfb = new LoginHotelFormBean();
        String comando = (String) request.getSession().getAttribute("comando");
        try {
            // Obs: BeanUtils é uma classe da biblioteca
            // Apache Commons BeanUtils
            // http://commons.apache.org/beanutils/
            BeanUtils.populate(lhfb, request.getParameterMap());
            HotelDAO hdao = new HotelDAO(dataSource);
            request.getSession().setAttribute("hotelLogado", lhfb);
            List<String> mensagens = lhfb.validar(hdao);    
            if (mensagens == null) {
                if(comando.equals("promocaoForm")){
                    SiteReservaDAO sdao = new SiteReservaDAO(dataSource);
                    List<SiteReserva> todosSites = sdao.listarTodosSiteReserva();
                    List<String> urls = new ArrayList<>();
                    for(int i=0; i<todosSites.size(); i++){
                        urls.add(todosSites.get(i).getUrl());
                        System.out.println(urls.get(i)+" "+todosSites.get(i).getUrl());
                    }
                    request.setAttribute("listaSites", todosSites);
                    request.setAttribute("listaUrls", urls);
                    comando = comando+".jsp";
                    request.getRequestDispatcher(comando).forward(request, response);
                }
                else if(comando.equals("listaPromocoesHotel")){
                    String enviar = "/EnviaCnpjServlet?comando="+comando;
                    request.getRequestDispatcher(enviar).forward(request,response);
                }
            } else {
                request.setAttribute("mensagens", mensagens);
                request.getRequestDispatcher("loginHotel.jsp").forward(request, response);
            }
        } catch (Exception e) {
            request.setAttribute("mensagem", e.getLocalizedMessage());
            request.getRequestDispatcher("erro.jsp").forward(request, response);
        }
    }
...

我的Jsp:

...
<ul class="urls"> 
<c:forEach items="${requestScope.listaUrls}" var="url"> 
<c:out value="${url}"/> 
</c:forEach> 
</ul>

<c:if test="${empty requestScope.listaUrls}">
<p>LIST IS EMPTY</p>
</c:if>
...

我明白了:

  

LIST IS EMPTY

0 个答案:

没有答案