jsp:forEach遍历结果集

 spring访问控制层controller中的实现:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jt.manage.pojo.User;
import com.jt.manage.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/findAll")
	public String findAll(Model model){
		List<User> userList = userService.findAll();
		//将数据传入页面中用于数据回显
		System.out.println(userList.toString());
		model.addAttribute("userList",userList);
		
		//后期经过视图
		return "userList";
	}
	
	@RequestMapping("/firstNew")
	public String firstNew(){
		return "NewFile";
	}
}

jsp文件中的实现:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!-- 引入Jsp标准标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
	<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>用户列表页面</title>
	</head>
	<body>
		<table width="60%" algin="center" border="1">
			<tr align="center">
				<td colspan="5"><h2>用户信息</h2></td>
			</tr>
			<tr align="center">
				<td>ID</td>
				<td>姓名</td>
				<td>年龄</td>
				<td>性别</td>
			</tr>
			<!-- 遍历返回结果 -->
			<c:forEach items="${userList}" var="u">
				<tr align="center">
					<td>${u.id}</td>
					<td>${u.name}</td>
					<td>${u.age }</td>
					<td>${u.sex }</td>
				</tr>
			</c:forEach>
		</table>
	</body>
</html>

 


版权声明:本文为qq_40725867原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。