吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1300|回复: 3
收起左侧

[Java 转载] 帮人解决springboot+thymeleaf问题

[复制链接]
fht000 发表于 2020-12-4 09:02
本帖最后由 fht000 于 2020-12-4 09:14 编辑

本帖主要用来回复这篇帖子的问题https://www.52pojie.cn/thread-1319246-1-1.html
@zxcvbnm12 希望对你有所帮助本来想在他本人的帖子上回复,但是无法在评论区回复中上传本地图片所以开了这个帖子

这是DepartmentDao :

[Java] 纯文本查看 复制代码
@Component
public class DepartmentDao {

    //模拟数据库中的数据
    private static Map<Integer, Department> departments = null;

    static {
        departments = new HashMap<Integer, Department>();//创建一个部门表
        departments.put(101, new Department(101, "部门表1"));
        departments.put(101, new Department(101, "部门表2"));
        departments.put(101, new Department(101, "部门表3"));
        departments.put(101, new Department(101, "部门表4"));
        departments.put(101, new Department(101, "部门表5"));
    }

    //获得所有部门信息
    public Collection<Department> getDepartments() {
        return departments.values();
    }

    //通过id得到部门
    public Department getDepartment(Integer id){
        return departments.get(id);
    }



    @AllArgsConstructor
    @Data
    public static class Department{
        private int id;
        private String departmentName;
   }
}


这是EmployeeDao:


[Java] 纯文本查看 复制代码
@Component
public class EmployeeDao {

    //模拟数据库中的数据
    private static Map<Integer,Employee> employees;

    @Autowired
    private DepartmentDao departmentDao;
    //员工有所属的部门
    static {
        employees = new HashMap<Integer, Employee>();//创建一个员工表
        employees.put(101,new Employee(101,"AA1","123@1123.com",1,new DepartmentDao.Department(101,"dwqdx")));
        employees.put(102,new Employee(102,"AA2","123@1123.com",1,new DepartmentDao.Department(102,"dwqdx")));
        employees.put(103,new Employee(103,"AA3","123@1123.com",1,new DepartmentDao.Department(103,"dwqdx")));
        employees.put(104,new Employee(104,"AA4","123@1123.com",1,new DepartmentDao.Department(104,"dwqdx")));
        employees.put(105,new Employee(105,"AA5","123@1123.com",1,new DepartmentDao.Department(105,"dwqdx")));
    }

    //主键自增!
    private static Integer initId=1006;
    //增加一个员工
    public void save(Employee employee){
        if(employee.getId() == 0){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }
    //查询全部员工信息
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //通过id查询员工
    public Employee getEmployeeById(Integer id)
    {
        return employees.get(id);
    }
    //删除员工通过id
    public void delete(Integer id){
        employees.remove(id);
    }

    @AllArgsConstructor
    @Data
    public static class Employee{
        private int id;
        private String lastName;
        private String email;
        private int gender;
        private DepartmentDao.Department department;
        private Date birth;

        public Employee(int id, String lastName, String email, int gender, DepartmentDao.Department department) {
            this.id = id;
            this.lastName = lastName;
            this.email = email;
            this.gender = gender;
            this.department = department;
        }

    }
}


这是EmployeeController:


[Java] 纯文本查看 复制代码
@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<EmployeeDao.Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "table";
    }
}


这是html:

[HTML] 纯文本查看 复制代码
<!DOCTYPE html>

<html lang="en" xmlns:th="http://thymeleaf.org">
<table class="table table-striped table-sm">
    <thead>
    <tr>
        <th>id</th>
        <th>lastname</th>
        <th>email</th>
        <th>gender</th>
        <th>department</th>
        <th>birth</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="emp:${emps}">
        <td th:text="${emp.id}"></td>

        <td>nihao</td>
        <td th:text="${emp.email}"></td>
        <td th:text="${emp.gender}"></td>
        <td th:text="${emp.department.departmentName}">11</td>
        <td th:text="${emp.lastName}"></td>
        <td >
            <button class="btn btn-sm btn-primary">编辑</button>
            <button class="btn btn-sm btn-danger">编辑</button>
        </td>
    </tr>
    </tbody>
</table>
</html>



这是properties:

[HTML] 纯文本查看 复制代码
server.port=8899

# Spring配置
spring.thymeleaf.mode=HTML
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8



这是pom:

[XML] 纯文本查看 复制代码
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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


运行后的结果:


重建个项目,复制粘贴总行了吧
QQ图片20201204084450.png

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| fht000 发表于 2020-12-4 10:27
怎么还没过审核啊
zxcvbnm12 发表于 2020-12-4 18:11
zxcvbnm12 发表于 2020-12-4 18:12
fht000 发表于 2020-12-4 10:27
怎么还没过审核啊

我先给你悬赏,一会吃完饭试一下嘿嘿,再次感谢你
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:22

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表