0

I'm newer in java. I need to iterate the news type in jsp page. I use <c:foreach> to do than in jsp page. In javaee Eclipse IDE I create a servlet to respose to jsp page. And create a newstype class in java to get and set newsid and newsname. Then in jsp loop newsid and newsname, code like:

<ul>
    <c:forEach var="type" items="${types}">
        <li><a href="#"><c:out value="${type.typeName}"/></a></li>
    </c:forEach>
</ul> 

There is the servlet code:

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

        newsservice ns = new newsservice();

        Map<Integer,String> types = ns.getNewsType();
        request.setAttribute("types",types);
        request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
    }

But open jsp page in chrome I got nothing. I'm concerned I'm do something wrong connect servlet and jsp, and miss something in code. Please somebody finger out. I'm sure import the taglib:

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

This is the snapshot: newspage project

1 Answers1

0

The expression type.typeName cannot be called on a Map (checkout the map API for methods you can call). You can loop over a Map however and get it's key and value.

Looks a lot like: Use <c:forEach> with HashMap

Community
  • 1
  • 1
ipper
  • 624
  • 4
  • 13
  • I'm tried 'type.value', but the result is the same. – TiglathPileser Apr 10 '17 at 10:16
  • What result exactly? Can you post the generated html that is sent to the browser? – ipper Apr 10 '17 at 13:40
  • And have you verified that types contains items at all? - add break point in doGet method right after you populate the items – ipper Apr 10 '17 at 13:43
  • I posted the code after the question. See snapshots. And I already debug the servlet doget method. It's totally fine . But I finally run in the servlet can get the result fine and run in Jsp page can't get the result . So I confused. Maybe I need to read more documents about that? – TiglathPileser Apr 10 '17 at 14:06