把Employees显示在页面上

时间:2025-02-18 16:33:44

项目代码下载:https://files.cnblogs.com/files/xiandedanteng/gatling20200429-1.zip

需求:从后台DB取出雇员数据,显示在前台页面上:

实现步骤

1.添加thymeleaf依赖。看到thymeleaf你是否记起它出现在那首著名的斯卡波罗菜市场呢?

<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.书写控制器

package com.ufo.gatling.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.ufo.gatling.entity.Emp;
import com.ufo.gatling.mapper.EmpMapper; @Controller
public class MvcCtrl {
@Autowired
private EmpMapper empMapper=null; // http://localhost:8080/listemps
@GetMapping("/listemps")
public ModelAndView index() {
List<Emp> list=empMapper.findAll(); ModelAndView mv=new ModelAndView("index");
mv.addObject("empList", list);
return mv;
} 。。。
}

3.在/gatling/src/main/resources/templates/下书写页面index.html,内容如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<caption>All employees</caption>
<thead>
<tr><th>id</th><th>name</th><th>salary</th></tr>
</thead>
<tbody>
<tr th:each="item:${empList}">
<td th:text="${item.id}">id</td>
<td th:text="${item.name}">name</td>
<td th:text="${item.salary}">salary</td>
</tr>
</tbody>
</table>
</body>
</html>

4.在浏览器中输入:http://localhost:8080/listemps,就会看到以下页面:

把Employees显示在页面上

--2020-04-29--