2013-04-09 4 views
8

Ich übergebe eine Liste an <c:forEach>, aber ich bekomme den Fehler, dass ich nicht weiß, wie ich darüber iterieren kann.JSTL-Nachricht: Ich weiß nicht, wie ich mit "forEach" über gelieferte "Items" iterieren kann

@RequestMapping("/viewall") 
public String viewAll(Model model) { 

    // productService.findAllProducts() returns List<Product> 
    model.addAttribute("everything", productService.findAllProducts()); 

    // Also tried using iterator, but I get same error 
    //model.addAtrribute("everything", productService.findAllProducts().iterator()); 

    .... 
} 

Die jsp Seite:

<c:forEach items="${everything}" var="prod"> 
    <p> ${prod.name} </p> 
</c:forEach> 

Der Fehler:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt; 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841) 
    org.apache.jsp.WEB_002dINF.jsp.allprod_jsp._jspService(allprod_jsp.java:158) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) 
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 

root cause 

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt; 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:255) 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:219) 
    org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:137) 
    javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:227) 
    org.apache.jsp.WEB_002dINF.jsp.allprod_jsp._jspx_meth_c_005fforEach_005f0(allprod_jsp.java:180) 
    org.apache.jsp.WEB_002dINF.jsp.allprod_jsp._jspService(allprod_jsp.java:137) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) 
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
+0

Was gibt 'productService.findAllProducts()' zurück? – BobTheBuilder

+0

Er sagte 'List ' –

+1

Sieht aus, als ob Sie ein anderes Attribut (Sitzung, Servletcontext) mit demselben Namen haben, der sich von einer 'List' unterscheidet. –

Antwort

9

Das Problem scheint zu sein, dass es eine andere Eigenschaft in Session oder ServletContext (Anwendung) mit dem gleichen Namen. Von JspContext#findAttribute (die das Verfahren bei der Verwendung von ${something} ausgeführt ist):

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

Es erklärt nicht die spezifische Reihenfolge, wie das Attribut gesucht, so könnte es herstellerspezifisch sein.

Um Probleme zu vermeiden, ändern Sie einfach den Namen Ihres Attributs.

2

Für Ihre Liste Schleifen über Sie so etwas wie dies tun können,

<c:forEach var="prod" begin="1" end="5"> 
     <option value="${prod}" >${prod.name}</option> 
    </c:forEach> 

Sie die Länge Ihrer Liste eine Bibliothek durch das Hinzufügen, die als jstl apis verwendet werden können,

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

bekommen und dann im Endattribut können Sie den Endwert als die Größe Ihrer Liste festlegen,

die endgültige co de wäre so etwas wie,

<c:forEach var="prod" begin="1" end="${fn:length(everything)}"> 
     <option value="${prod.name}" >${l}</option> 
    </c:forEach> 

dieser Code stellen Sie die Schleifenvariable, um zu starten Wert als 1 und Endwert als Listengröße beginnen.

+1

Entschuldigen Sie, um zu entmutigen, aber OP's Problem war nicht, wie man die Schleife iteriert, die Ursache war, dass es ein anderes Attribut in der Sitzung oder ServletContext mit dem gleichen Namen gab.Das Umbenennen des Attributs löste das Problem (wie in meiner Antwort angegeben). –