0

I'm trying to have my JSP page cleanly go through an ArrayList of data objects, and for each one, insert the contents into a piece of HTML. Even for a simple page, it fails to work correctly. I'm placing the ArrayList into the HttpServletRequest object as an attribute, but:

  • trying to use jsp:UseBean fails to work, as it won't let me declare it either as an ArrayList or an ArrayList-String.

  • trying to use JSTL and EL doesn't work, as the renderer completely ignores my ${articleList}.

I'm clearly doing something wrong, but I'm not sure what. Have I misconfigured JSTL and EL? Am I using something way too complex to do something that could be more easily achieved another way?

My controller:

public void doGet(  HttpServletRequest request,
            HttpServletResponse response) 
            throws ServletException, IOException {
        //index
        if (request.getContextPath() == "") {
            request.setAttribute("articleList", getArticles());
            RequestDispatcher dp = request.getRequestDispatcher("/index.jsp");
            dp.forward(request, response);
        }

        RequestDispatcher dp = request.getRequestDispatcher("/404.html");
        dp.forward(request, response);
    }

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WSD Newspaper</title>
</head>
<body>

    <%@ include file="WEB-INF/login_fragment.jsp" %>
    <%@ include file="WEB-INF/header.jsp" %>

<span id="articles">
    <c:forEach var="article" items="${articleList}">
    <span class="article">
        <h1>${article.title}</h1>
        <div class="byline">by ${article.author.name}</div>
        <p class="short-text"> ${article.shortText}</p>
        <p><a href="article/${article.id}">Read More...</a></p>
    </span>
    </c:forEach>
</span>
</body>
</html>
Merus
  • 8,796
  • 5
  • 28
  • 41
  • Firstly, does your if condition actually execute (http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Secondly does getArticles() actually return data. The actual .jsp itself looks okay. – Alan Hay May 21 '15 at 13:59
  • Yes; I get HTML from index.jsp back from the index page. I know that EL isn't working because I've already tested passing a string as a request attribute, and it doesn't show up either. – Merus May 22 '15 at 00:42
  • Well there are lots of discussions on here about EL not working. http://stackoverflow.com/questions/2168832/expression-language-in-jsp-not-working for example. – Alan Hay May 22 '15 at 08:00

0 Answers0