在JSP页面中使用对象的ArrayList的最佳方法是什么?

时间:2015-05-21 12:57:31

标签: jsp jstl el

我正在尝试让我的JSP页面干净地遍历数据对象的ArrayList,并且对于每个JSP页面,将内容插入到一段HTML中。即使对于简单的页面,它也无法正常工作。我将ArrayList作为属性放入HttpServletRequest对象中,但是:

  • 尝试使用jsp:UseBean无法正常工作,因为它不会让我将它声明为ArrayList或ArrayList-String。

  • 尝试使用JSTL和EL不起作用,因为渲染器完全忽略了我的$ {articleList}。

我显然做错了什么,但我不确定是什么。我是否错误配置了JSTL和EL?我是否使用过于复杂的东西来做一些可以通过另一种方式更容易实现的事情?

我的控制器:

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>

0 个答案:

没有答案