JSTL 中使用foeach嵌套循环

很长时间没有写过jsp代码了,今天碰到一个使用jstl froeach循环的问题,
需求是这样的:
每个用户都有多个收货地址,再jsp中将这些用户对应的收货地址展示出来
下面我做的一个小例子
servlet代码:
Address address1 = new Address();
		address1.setId(1);
		address1.setName("地址1");
		address1.setPhone("123");
		address1.setPostcode("456");
		Address address2 = new Address();
		address2.setId(2);
		address2.setName("地址2");
		address2.setPhone("123");
		address2.setPostcode("456");
		
		List list = new ArrayList<Address>();
		list.add(address1);
		list.add(address2);
		
		User user1 = new User();
		user1.setId(1);
		user1.setUsername("user1");
		user1.setNickname("papa");
		user1.setPassword("123123");
		user1.setType(4);
		user1.setAddresses(list);
		
		User user2 = new User();
		user2.setId(1);
		user2.setUsername("user2");
		user2.setNickname("papa");
		user2.setPassword("123123");
		user2.setType(4);
		user2.setAddresses(list);
		
		List userList = new ArrayList<User>();
		userList.add(user2);
		userList.add(user1);
		request.setAttribute("userList", userList);
		RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
		rd.forward(request, response);


jsp中的代码
<c:forEach var="user" items="${userList }">
    	${user.username }
    	<ul>
	  		<c:forEach var="addresse" items="${user.addresses}">
	    		<li>${addresse.id }</li>
	    		<li>${addresse.name }</li>
	    	</c:forEach>
    	</ul>
    </c:forEach>

你可能感兴趣的:(jstl)