如何在jsp中发送一个对象数组

时间:2016-09-13 08:05:47

标签: java jsp servlets web-applications jstl

我在servlet中有以下方法

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("categories", categoryService.getAll());

        RequestDispatcher dispatcher = request.getRequestDispatcher("/Category.jsp");
        dispatcher.forward(request, response);
        }

当我打电话给jsp页面时,我想从get获取所有类别,但它根本不会调用get方法

这是我的jsp

<body>
<% List<CategoryDTO> categoryList=new ArrayList<CategoryDTO>(); 
categoryList=(ArrayList<CategoryDTO>)request.getAttribute("categories");
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:forEach items="${categoryList}" var="element"> 
  <tr>
    <td>${element.name}</td>

  </tr>
</c:forEach

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

无需在jsp中声明列表,只需尝试这样,

<body>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:forEach items="${categories}" var="element"> //change here categories not categotyList
  <tr>
    <td>${element.name}</td>

  </tr>
</c:forEach