Springboot

一、thymeleaf

文档地址:Tutorial: Using Thymeleaf

模范

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  </head>

  <body>
  
    <p th:text="#{home.welcome}">Welcome to our grocery store!</p>
  
  </body>

</html>

(1)导入约束

<html xmlns:th="http://www.thymeleaf.org">

二、国际化标准(i18n)

1.login.properties   login_en_US.properties

2、切换中英文

<a class="btn btn-sm" th:href="@{/(l='zh_CN')}">中文</a>
  <a class="btn btn-sm" th:href="@{/(l='en_US')}">English</a>

3、在apllication.properties注册bean

spring.messages.basename=i18n.login

三、CRUD

1、Controller

@Controller
public class StaffController {
    @Autowired
    StaffDao staffDao;
    @Autowired
    DepartmentDao departmentDao;
    @RequestMapping("/AllEmp")
    public String getAll(Model model){
        //获取所有员工信息
        Collection<Staff> staffs=staffDao.getAll();
        model.addAttribute("emps",staffs);
        return "emp/list";
    }
    @GetMapping ("/add")
    public String toAddPage(Model model){
        //查出所有部门的信息
        Collection<Department> departments = departmentDao.getAllDepartment();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/add")
    public String addEmp(Staff staff){
        //添加员工页面
        staffDao.save(staff);
        return "redirect:/AllEmp";
    }
    @GetMapping("/update/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        //查出修改员工信息
        Staff staff = staffDao.getStaffById(id);
        model.addAttribute("emp",staff);
        //查出所有部门的信息
        Collection<Department> departments = departmentDao.getAllDepartment();
        model.addAttribute("departments",departments);
        return "/emp/update";
    }
    @PostMapping("/update")
    public String update(Staff staff){
        staffDao.save(staff);
        return "redirect:/AllEmp";
    }
    @GetMapping("/delete/{id}")
    public String deleteEmp(@PathVariable("id") Integer id,Model model){
        model.addAttribute("emp",staffDao.getStaffById(id));
        staffDao.delete(id);
        return "redirect:/AllEmp";
    }

}

2、

三、前端减少代码冗余

1、提取(th:fragment="name")

<!--顶部导航栏-->
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow" th:fragment="topbar">
    <a class="navbar-brand col-md-3 col-lg-2 mr-0 px-3" href="#" name="session">YOU</a>
    <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-toggle="collapse" data-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
    <ul class="navbar-nav px-3">
        <li class="nav-item text-nowrap">
            <a class="nav-link" th:href="@{/login}">登出</a>
        </li>
    </ul>
</nav>

2、插入(common为提取文件名)

<div th:replace="~{common::name}"></div>

3、for each循环(departments为后台返回数据名)

th:each="dpt:${departments}" 
th:text="${dpt.getDepartmentName()}" 

4、日期格式化

th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}"
上一篇:继承


下一篇:大数据之Sqoop