fht000 发表于 2020-12-4 09:02

帮人解决springboot+thymeleaf问题

本帖最后由 fht000 于 2020-12-4 09:14 编辑

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

这是DepartmentDao :

@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:


@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:


@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:

<!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:

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:

<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>

运行后的结果:
https://attach.52pojie.cn//forum/202012/04/085921cennqn6e2eejzzpg.png?l

重建个项目,复制粘贴总行了吧

fht000 发表于 2020-12-4 10:27

怎么还没过审核啊

zxcvbnm12 发表于 2020-12-4 18:11

fht000 发表于 2020-12-4 10:27
怎么还没过审核啊

大佬我看到了:lol

zxcvbnm12 发表于 2020-12-4 18:12

fht000 发表于 2020-12-4 10:27
怎么还没过审核啊

我先给你悬赏,一会吃完饭试一下嘿嘿,再次感谢你
页: [1]
查看完整版本: 帮人解决springboot+thymeleaf问题