如何用themeleaf显示图片

一.数据库中:

存的是图片的绝对路径

二.后端查询出该对象

@Controller
public class EmpController {
          
   
    @Autowired
    private EmpService empService;

    @RequestMapping("/getOne")
    public String emp(Integer did,Model model){
          
   
        List<Emp> emps = empService.selectEmpByDid(did);

        model.addAttribute("emps",emps);
        return "emp";
    }
}

三.前端用thymeleaf展示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1" style="border-collapse: collapse">

    <tr>
        <th>ID</th>
        <th>员工名称</th>
        <th>头像</th>
    </tr>
    <tr th:each="emp:${emps}">
        <td th:text="${emp.id}"></td>
        <td th:text="${emp.name}"></td>
        <!--展示图片-->
        <td><img th:src="@{${emp.photo}}" th:width="50px" th:height="50px"/></td>

    </tr>
</table>

</body>
</html>
经验分享 程序员 微信小程序 职场和发展