Announcement Announcement Module
Collapse
No announcement yet.
Nested <c:foreach> Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nested <c:foreach>

    defaultMenu.jsp
    Code:
    <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
    
    <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
    
    <c:forEach items="${sampleList}" var="menu">
    	<ul>
    		<li>${menu.link}</li>
    		<c:set var="sublist" value="menu.submenu" />
    		<ul>
    			<c:forEach items="sublist" var="submenu">
    				<li>${submenu.link}</li>
    			</c:forEach>
    		</ul>
    	</ul>
    </c:forEach>
    MenuController
    Code:
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		
    		logger.info("Initializing Menu");
    		List<Menu> sampleList = new ArrayList<Menu>();
    		List<SubMenu> subMenuList = new ArrayList<SubMenu>();
    		for (int i = 0; i < 15; i++) {
    			Menu menu = new Menu();
    			menu.setLink("Menu " + i);
    			for (int x = 0; x < 10; x++) {
    				SubMenu subMenu = new SubMenu();
    				subMenu.setLink("SubMenu " + x);
    			}
    			menu.setSubmenu(subMenuList);
    			sampleList.add(menu);
    		}
    		
    		return new ModelAndView("defaultMenu", "sampleList", sampleList);
    	}
    Menu.java
    Code:
    public class Menu{
    	private String link;
    	private List<SubMenu> submenu;
    
    	public String getLink() {
    		return link;
    	}
    
    	public List<SubMenu> getSubmenu() {
    		return submenu;
    	}
    
    	public void setLink(String link) {
    		this.link = link;
    	}
    
    	public void setSubmenu(List<SubMenu> submenu) {
    		this.submenu = submenu;
    	}
    }
    SubMenu.java
    Code:
    public class SubMenu extends BaseModel {
    	private String link;
    
    	public String getLink() {
    		return link;
    	}
    
    	public void setLink(String link) {
    		this.link = link;
    	}
    
    }
    How do I access a List inside another list in Spring? I would like to have the effect similar below
    HTML Code:
    <ul>
    	<li>Menu 0</li>
    		
    	<ul>
    			
    		<li>SubMenu 0</li>
    		<li>SubMenu 1</li>
    		<li>SubMenu 2</li>
    		<li>SubMenu 3</li>
    		<li>SubMenu 4</li>
    		<li>SubMenu 5</li>
    		<li>SubMenu 6</li>
    		<li>SubMenu 7</li>
    		<li>SubMenu 8</li>
    		<li>SubMenu 9</li>
    			
    	</ul>
    </ul>
    However, I am getting an error
    Code:
    javax.el.PropertyNotFoundException: Property 'link' not found on type java.lang.String
    	javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:193)
    	javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:170)
    	javax.el.BeanELResolver.property(BeanELResolver.java:279)
    	javax.el.BeanELResolver.getValue(BeanELResolver.java:60)
    	javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    	org.apache.el.parser.AstValue.getValue(AstValue.java:118)
    	org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    	org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
    	org.apache.jsp.pages.template.tiles.defaultMenu_jsp._jspx_meth_c_005fforEach_005f1(defaultMenu_jsp.java:168)
    	org.apache.jsp.pages.template.tiles.defaultMenu_jsp._jspx_meth_c_005fforEach_005f0(defaultMenu_jsp.java:104)
    	org.apache.jsp.pages.template.tiles.defaultMenu_jsp._jspService(defaultMenu_jsp.java:62)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
    	org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
    If I remove the inner <c:forEach>, it works fine, however, it only iterates the Menu part.
    Last edited by raseryu; Aug 23rd, 2009, 09:10 PM.

  • #2
    raseryu,

    Not sure what's going on, but have tried using a different taglib declaration?

    For 2.4/2.5:

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

    Code:
    <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
    AFAIK, using the *_rt taglibs is not recommended.

    Hope that helps,

    Comment

    Working...
    X