springboot+thymeleaf:后端传递多个对象(List<User>)到前端

这里已经配置好了springboot+thymeleaf+mybatis-plus
这里仅提及到主要代码
先看controller类,这里是以查询全部员工为例
Employeemapper继承basemapper,从而调用一般的crud。查询全部也很简单使用List<Employee> list = employeeMapper.selectList(null);即可,但怎么将这里的list传递到HTML页面,尝试了很多。一开始使用model进行传递model.addAttribute("list", employeeMapper.selectList(null));但是HTML中拿不到值,也试过modelandview,同样拿不到值。尝试用model传递单个对象(Employee/Person),发现是可以成功取到值的。最后使用request(session没测,但应该可以)才成功将List传到html显示

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.bean.Employee;
import com.example.demo.bean.Person;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.service.EmployeeService;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeMapper employeeMapper;
	
	@RequestMapping("selectAll")
	public String  selectAll(HttpServletRequest request) {
		List<Employee> list = employeeMapper.selectList(null);
		request.setAttribute("users", list);
//		model.addAttribute("list", employeeMapper.selectList(null));
//		Person person = new Person("zs", 11);
//		model.addAttribute(person);
//		Employee employee = new Employee(1, "zs", 11, "123@qq.com", 1);
//		model.addAttribute(employee);
		return "info";
	}
}

前端HTML页面
注意使用thymeleaf模板引擎要在HTML标签中加上xmlns:th="http://www.thymeleaf.org"
循环用th:each属性
th:each属性用于迭代循环,语法:th:each=“obj,xxState:${objList}”
迭代对象可以是java.util.List,java.util.Map,数组等;
iterStat称作状态变量,属性有:
index:当前迭代对象的index(从0开始计算)
count: 当前迭代对象的index(从1开始计算)
size:被迭代对象的大小
current:当前迭代变量
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
first:布尔值,当前循环是否是第一个
last:布尔值,当前循环是否是最后一个
根据情况xxState可以省略
可以看到下面有两种取值
第一种用于一般取值()
第二种用于取出对象属性

<td th:text="${user.id}">1</td>
<td th:text="${user.getId()}">1</td>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>所有员工信息页面</title>
</head>
<body>
	<table>
		<tr>
			<th>ID</th>
			<th>名字</th>
			<th>邮箱</th>
			<th>性别</th>
			<th>年龄</th>
		</tr>
		<tr th:each="user:${users}">
		<!-- 
			<td th:text="${user.id}">1</td>
			<td th:text="${user.lastName}">2</td>
			<td th:text="${user.email}">3</td>
			<td th:text="${user.gender}">4</td>
			<td th:text="${user.age}">5</td>
		-->
			<td th:text="${user.getId()}">1</td>
			<td th:text="${user.getlastName()}">2</td>
			<td th:text="${user.getEmail()}">3</td>
			<td th:text="${user.getGender()}">4</td>
			<td th:text="${user.getAge()}">5</td>
			</tr>
	</table>
</body>
</html>

有写的不准确或需补充的,欢迎各位路过的大佬指出

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值