SpringBoot

  • (十三)、员工管理系统SpringBoot
    • 1.准备工作
      • (1).导入我们所需要的环境依赖
      • (2).首页的Controller与View (静态资源Thymeleaf接管)
    • 2.国际化
      • (1).设置字符编码为UTF-8
      • (2).添加文件资源目录 i18n
      • (3).注册国际化实现
      • (4).国际化的实现 index.html
      • (5).英文与汉文的交互
      • (6).在springBoot进行配置实现
    • 3.登入功能实现
      • (1).设置登入的controller
      • (2).设置登入的 html
    • 4.登入拦截器
      • (1).设置Java类
      • (2).配置拦截器在SpringBoot
    • 5.什么情况下需要在SpringBoot里面使用@Bena
    • 6.展示员工列表
      • (1).上边框的复用 (insert)
      • (2).上边框的复用 (replace)
      • (3).设置高亮的数据
      • (4).设置controller层
      • (5).list.html进行遍历
    • 7.添加员工
      • (1).重定向注意事项
      • (2).添加员工的页面
      • (3).controller层的实现
      • (4).解决疑惑
    • 8.修改员工与删除员工
      • (1).update.html页面
      • (2).list.html页面
      • (3).controller业务层

(十三)、员工管理系统SpringBoot

1.准备工作

(1).导入我们所需要的环境依赖

我们要导入依赖Thymeleaf

<!--     Thymeleaf   --><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId><version>2.7.7</version></dependency>

导入配置语句

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


(1).html页面要放在template这个目录下。(2).css js img 要放在static这个目录下 (3).icon图标自定义要放在 public 这个目录下

(2).首页的Controller与View (静态资源Thymeleaf接管)

Index.html
在这里我们要知道: 我们如果写thymeleaf的语法。必须先要导入配置以及编写配置文件,并且在使用的属性上要进行添加 th 的特征。如果是链接需要 @{xxxx}

<!DOCTYPE html>
<html lang="en" xmlns:th="http:/"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Signin Template for Bootstrap</title><!-- Bootstrap core CSS -->
<!--我们的Themleafe的写法是: 标志th:  并且链接用 @{}      --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/signin.css}" rel="stylesheet"></head><body class="text-center"><form class="form-signin" action="dashboard.html"><img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal">Please sign in</h1><label class="sr-only">Username</label><input type="text" class="form-control" placeholder="Username" required="" autofocus=""><label class="sr-only">Password</label><input type="password" class="form-control" placeholder="Password" required=""><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"> Remember me</label></div><button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><a class="btn btn-sm">中文</a><a class="btn btn-sm">English</a></form></body></html>

**Controller**层实现视图解析器
这个实现视图跳转的话,我们需要借助于thymeleaf进行跳转

package com.jsxs.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller  //设置controller层的注解
public class IndexController {@RequestMapping({"/","/index.html"})   //就是说:  我们如果访问单独的一个"/" 或则 单独的一个 "/index.html". 都会跳转到首页面。 我们用数据进行框起来。public String index(){return "index";}
}

MyMvcConfig通过更改SpringBoot配置实现跳转。 (1).我们类的名字必须要写成MyMvcConfig.(2).添加注解 @Configuration

package com.jsxs.config;//全面扩展 springmvcimport org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;// 我们要重新配置MyMvcConfig, 我们要设置Configuration这个注解@Configurationpublic class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");}
}

2.国际化

(1).设置字符编码为UTF-8

(2).添加文件资源目录 i18n

我们在resources 根目录下设置一个 i18n的文件夹

然后设置两个文件资源 properties

可视化的操作, 我们只需要设置一个就会对应两个更改

(3).注册国际化实现

源码

# 配置国际化 :  这里的作用就是与我们设置的中文配置和英文配置进行绑定,这里的login 就是login.properties的文件名
spring.messages.basename=i18n.login

(4).国际化的实现 index.html

  • 国际化: #{xxxx}
  • 变量信息 : ${xxxx}
  • 路径 : @{/xxxx}
    注意我们要使用 th: 属性 进行数据的绑定。并且在a标签中我们要想后端传值
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Signin Template for Bootstrap</title><!-- Bootstrap core CSS -->
<!--我们的Themleafe的写法是: 标志th:  并且链接用 @{}      --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/signin.css}" rel="stylesheet"></head><body class="text-center"><form class="form-signin" th:action="@{/user/login}"><img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<!-- 假如说: msg的内容不为空我们就输出语句,否则     --><h1 style="color: red; font-size: 24px;font-family: 楷体" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></h1><label class="sr-only" th:text="#{login.username}">Username</label><input type="text" class="form-control" placeholder="Username" name="username" required="" autofocus="" th:placeholder="#{login.username}"><label class="sr-only" th:text="#{login.password}">Password</label><input type="password" class="form-control" placeholder="Password" name="password" required="" th:placeholder="#{login.password}"><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a><a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></form></body>
</html>

(5).英文与汉文的交互

这里我们要进行继承本地解析器. (1)、我们获取前端传进来的参数,并且对其进行分割。第一段位于岩第二段为国家,并且返回国家的信息

package com.jsxs.config;import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;// 我们继承
public class MyLocaleResolver implements LocaleResolver {//  解析请求@Overridepublic Locale resolveLocale(HttpServletRequest request) {//         获取请求中的语言参数String language = request.getParameter("l");
//     有过没有就使用默认的Locale locale = Locale.getDefault();
//        如果请求的链接携带了国家化的参数。if(!StringUtils.isEmpty(language)){String[] split = language.split("_");
//             语言 和 国家 分别对应Locale locale1 = new Locale(split[0], split[1]);return locale1;}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {}
}


(6).在springBoot进行配置实现

修改默认配置

package com.jsxs.config;//全面扩展 springmvcimport org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;// 我们要重新配置MyMvcConfig, 我们要设置Configuration这个注解@Configurationpublic class MyMvcConfig implements WebMvcConfigurer {//    对视图跳转进行生效@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");registry.addViewController("/main.html").setViewName("dashboard");}//    把自定义的国际化组件生效的操作@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}

3.登入功能实现

(1).设置登入的controller

假如说: 我们的密码和账户验证正确,那么我们就进行主界面,如果没有成功,那么我们就返回一个msg语句,提示用户密码错误

  • StringUtils.hasText(username) 假如username为空
package com.jsxs.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpSession;@Controller
public class LoginController {@RequestMapping("/user/login")public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){//        假如说: username不为空且passwordif (StringUtils.hasText(username)&&username.equals("admin")&&password.equals("123456")&&StringUtils.hasText("password")){return "dashboard";}else {model.addAttribute("msg","用户名或则密码错误");return "index";}}
}

(2).设置登入的 html

(1).这里我们更新了 form表单的路径.(2).msg的设置 (3). th: if 可以进行显示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Signin Template for Bootstrap</title><!-- Bootstrap core CSS -->
<!--我们的Themleafe的写法是: 标志th:  并且链接用 @{}      --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/signin.css}" rel="stylesheet"></head><body class="text-center"><form class="form-signin" th:action="@{/user/login}"><img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<!-- 假如说: msg的内容不为空我们就输出语句,否则     --><h1 style="color: red; font-size: 24px;font-family: 楷体" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></h1><label class="sr-only" th:text="#{login.username}">Username</label><input type="text" class="form-control" placeholder="Username" name="username" required="" autofocus="" th:placeholder="#{login.username}"><label class="sr-only" th:text="#{login.password}">Password</label><input type="password" class="form-control" placeholder="Password" name="password" required="" th:placeholder="#{login.password}"><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a><a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></form></body>
</html>


4.登入拦截器

我们先对一些数据进行拦截,需要继承程序处理器。 假如存在Session我们就对其进行获取。

(1).设置Java类

package com.jsxs.config;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class LoginHandlerInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//        登入成功之后存在登入后的sessionif (request.getSession().getAttribute("LoginUser")!=null){return true;}else {request.setAttribute("msg","没有权限请先进行登入");request.getRequestDispatcher("/index.html").forward(request,response);return false;}}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {HandlerInterceptor.super.afterCompletion(request, response, handler, ex);}
}

(2).配置拦截器在SpringBoot

配置拦截器

  • 拦截器拦截所有请求,排除首页页面,以及我们点击登入跳转的哪个页面 和 静态资源中的assert下的所有
  registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/asserts/*");
package com.jsxs.config;//全面扩展 springmvcimport org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;// 我们要重新配置MyMvcConfig, 我们要设置Configuration这个注解@Configurationpublic class MyMvcConfig implements WebMvcConfigurer {//    对视图跳转进行生效@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");registry.addViewController("/main.html").setViewName("dashboard");}//    把自定义的国际化组件生效的操作@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}//    对拦截器进行生效@Overridepublic void addInterceptors(InterceptorRegistry registry) {//    拦截器拦截所有请求,排除首页页面,以及我们点击登入跳转的哪个页面 和 静态资源中的assert下的所有registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/asserts/*");}
}


5.什么情况下需要在SpringBoot里面使用@Bena

  1. 假如说我们继承的WebMvcConfigurer的重写方法里面存在这个方法。那么我们就不适用@Bean
  2. 除了以上情况我们都要 @Bean

6.展示员工列表

(1).上边框的复用 (insert)

我们通过设置 th:fragment="xxx",进行复用

th:fragment="topbar"

dashboard.html

<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏 --><nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar"><a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">[[${session.LoginUser}]]</a><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" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a></li></ul></nav><div class="container-fluid"><div class="row"><nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="silderbar"><div class="sidebar-sticky"><ul class="nav flex-column"><li class="nav-item"><a class="nav-link active" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path><polyline points="9 22 9 12 15 12 15 22"></polyline></svg>Dashboard <span class="sr-only">(current)</span></a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file"><path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline></svg>Orders</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>Products</a></li><li class="nav-item"><a class="nav-link" th:href="@{/emps}"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle><path d="M23 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path></svg>员工管理</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bar-chart-2"><line x1="18" y1="20" x2="18" y2="10"></line><line x1="12" y1="20" x2="12" y2="4"></line><line x1="6" y1="20" x2="6" y2="14"></line></svg>Reports</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-layers"><polygon points="12 2 2 7 12 12 22 7 12 2"></polygon><polyline points="2 17 12 22 22 17"></polyline><polyline points="2 12 12 17 22 12"></polyline></svg>Integrations</a></li></ul><h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted"><span>Saved reports</span><a class="d-flex align-items-center text-muted" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h6><ul class="nav flex-column mb-2"><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Current month</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Last quarter</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Social engagement</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Year-end sale</a></li></ul></div></nav><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><div class="chartjs-size-monitor" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;"><div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div></div><div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:200%;height:200%;left:0; top:0"></div></div></div><div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom"><h1 class="h2">Dashboard</h1><div class="btn-toolbar mb-2 mb-md-0"><div class="btn-group mr-2"><button class="btn btn-sm btn-outline-secondary">Share</button><button class="btn btn-sm btn-outline-secondary">Export</button></div><button class="btn btn-sm btn-outline-secondary dropdown-toggle"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>This week</button></div></div><canvas class="my-4 chartjs-render-monitor" id="myChart" width="1076" height="454" style="display: block; width: 1076px; height: 454px;"></canvas></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}" ></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}" ></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}" ></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

进行复用接受: ~{被插入页面的名字::th:fragment的名字}

<div th:insert="~{dashboard::topbar}"></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏   --><div th:insert="~{dashboard::topbar}"></div><div class="container-fluid"><div class="row"><!--      设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:insert="~{dashboard::silderbar}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2>Section title</h2><div class="table-responsive"><table class="table table-striped table-sm"><thead><tr><th>#</th><th>Header</th><th>Header</th><th>Header</th><th>Header</th></tr></thead><tbody><tr><td>1,001</td><td>Lorem</td><td>ipsum</td><td>dolor</td><td>sit</td></tr><tr><td>1,002</td><td>amet</td><td>consectetur</td><td>adipiscing</td><td>elit</td></tr><tr><td>1,003</td><td>Integer</td><td>nec</td><td>odio</td><td>Praesent</td></tr><tr><td>1,003</td><td>libero</td><td>Sed</td><td>cursus</td><td>ante</td></tr><tr><td>1,004</td><td>dapibus</td><td>diam</td><td>Sed</td><td>nisi</td></tr><tr><td>1,005</td><td>Nulla</td><td>quis</td><td>sem</td><td>at</td></tr><tr><td>1,006</td><td>nibh</td><td>elementum</td><td>imperdiet</td><td>Duis</td></tr><tr><td>1,007</td><td>sagittis</td><td>ipsum</td><td>Praesent</td><td>mauris</td></tr><tr><td>1,008</td><td>Fusce</td><td>nec</td><td>tellus</td><td>sed</td></tr><tr><td>1,009</td><td>augue</td><td>semper</td><td>porta</td><td>Mauris</td></tr><tr><td>1,010</td><td>massa</td><td>Vestibulum</td><td>lacinia</td><td>arcu</td></tr><tr><td>1,011</td><td>eget</td><td>nulla</td><td>Class</td><td>aptent</td></tr><tr><td>1,012</td><td>taciti</td><td>sociosqu</td><td>ad</td><td>litora</td></tr><tr><td>1,013</td><td>torquent</td><td>per</td><td>conubia</td><td>nostra</td></tr><tr><td>1,014</td><td>per</td><td>inceptos</td><td>himenaeos</td><td>Curabitur</td></tr><tr><td>1,015</td><td>sodales</td><td>ligula</td><td>in</td><td>libero</td></tr></tbody></table></div></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

(2).上边框的复用 (replace)

commons包下的commons.html

  • th:fragment
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><!-- 顶部上边--><nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar"><a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">[[${session.LoginUser}]]</a><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" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a></li></ul>
</nav><!--侧边栏-->
<nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="silderbar"><div class="sidebar-sticky"><ul class="nav flex-column"><li class="nav-item"><a class="nav-link active" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path><polyline points="9 22 9 12 15 12 15 22"></polyline></svg>Dashboard <span class="sr-only">(current)</span></a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file"><path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline></svg>Orders</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>Products</a></li><li class="nav-item"><a class="nav-link" th:href="@{/emps}"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle><path d="M23 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path></svg>员工管理</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bar-chart-2"><line x1="18" y1="20" x2="18" y2="10"></line><line x1="12" y1="20" x2="12" y2="4"></line><line x1="6" y1="20" x2="6" y2="14"></line></svg>Reports</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-layers"><polygon points="12 2 2 7 12 12 22 7 12 2"></polygon><polyline points="2 17 12 22 22 17"></polyline><polyline points="2 12 12 17 22 12"></polyline></svg>Integrations</a></li></ul><h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted"><span>Saved reports</span><a class="d-flex align-items-center text-muted" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h6><ul class="nav flex-column mb-2"><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Current month</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Last quarter</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Social engagement</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Year-end sale</a></li></ul></div>
</nav>
</body>
</html>

dashbord.html

  • 都必须要用div
<div th:replace="~{commons/commons::topbar}"></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏 -->
<!-- 顶部导航栏   -->
<div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row">
<!-- 侧边栏看-->
<!--   设置插入语句,  ~{跟源html的名字:: th:fragment的名字 }           --><div th:replace="~{commons/commons::silderbar}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><div class="chartjs-size-monitor" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;"><div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div></div><div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:200%;height:200%;left:0; top:0"></div></div></div><div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom"><h1 class="h2">Dashboard</h1><div class="btn-toolbar mb-2 mb-md-0"><div class="btn-group mr-2"><button class="btn btn-sm btn-outline-secondary">Share</button><button class="btn btn-sm btn-outline-secondary">Export</button></div><button class="btn btn-sm btn-outline-secondary dropdown-toggle"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>This week</button></div></div><canvas class="my-4 chartjs-render-monitor" id="myChart" width="1076" height="454" style="display: block; width: 1076px; height: 454px;"></canvas></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}" ></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}" ></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}" ></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

list.html

<div th:replace="~{commons/commons::topbar}"></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏   --><div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2>Section title</h2><div class="table-responsive"><table class="table table-striped table-sm"><thead><tr><th>#</th><th>Header</th><th>Header</th><th>Header</th><th>Header</th></tr></thead><tbody><tr><td>1,001</td><td>Lorem</td><td>ipsum</td><td>dolor</td><td>sit</td></tr><tr><td>1,002</td><td>amet</td><td>consectetur</td><td>adipiscing</td><td>elit</td></tr><tr><td>1,003</td><td>Integer</td><td>nec</td><td>odio</td><td>Praesent</td></tr><tr><td>1,003</td><td>libero</td><td>Sed</td><td>cursus</td><td>ante</td></tr><tr><td>1,004</td><td>dapibus</td><td>diam</td><td>Sed</td><td>nisi</td></tr><tr><td>1,005</td><td>Nulla</td><td>quis</td><td>sem</td><td>at</td></tr><tr><td>1,006</td><td>nibh</td><td>elementum</td><td>imperdiet</td><td>Duis</td></tr><tr><td>1,007</td><td>sagittis</td><td>ipsum</td><td>Praesent</td><td>mauris</td></tr><tr><td>1,008</td><td>Fusce</td><td>nec</td><td>tellus</td><td>sed</td></tr><tr><td>1,009</td><td>augue</td><td>semper</td><td>porta</td><td>Mauris</td></tr><tr><td>1,010</td><td>massa</td><td>Vestibulum</td><td>lacinia</td><td>arcu</td></tr><tr><td>1,011</td><td>eget</td><td>nulla</td><td>Class</td><td>aptent</td></tr><tr><td>1,012</td><td>taciti</td><td>sociosqu</td><td>ad</td><td>litora</td></tr><tr><td>1,013</td><td>torquent</td><td>per</td><td>conubia</td><td>nostra</td></tr><tr><td>1,014</td><td>per</td><td>inceptos</td><td>himenaeos</td><td>Curabitur</td></tr><tr><td>1,015</td><td>sodales</td><td>ligula</td><td>in</td><td>libero</td></tr></tbody></table></div></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>


(3).设置高亮的数据

commons.html这
我们利用三元运算符,判断是否高亮

<a th:class="${active=='main.html' ? 'nav-link active' : 'nav-link'}" th:href="@{/index.html}">
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><!-- 顶部上边--><nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar"><a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">[[${session.LoginUser}]]</a><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" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a></li></ul>
</nav><!--侧边栏-->
<nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="silderbar"><div class="sidebar-sticky"><ul class="nav flex-column"><li class="nav-item"><a th:class="${active=='main.html' ? 'nav-link active' : 'nav-link'}" th:href="@{/index.html}"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path><polyline points="9 22 9 12 15 12 15 22"></polyline></svg>首页 <span class="sr-only">(current)</span></a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file"><path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline></svg>Orders</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>Products</a></li><li class="nav-item"><a th:class="${active=='list.html' ? 'nav-link active' : 'nav-link'}" th:href="@{/emps}"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path><circle cx="9" cy="7" r="4"></circle><path d="M23 21v-2a4 4 0 0 0-3-3.87"></path><path d="M16 3.13a4 4 0 0 1 0 7.75"></path></svg>员工管理</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bar-chart-2"><line x1="18" y1="20" x2="18" y2="10"></line><line x1="12" y1="20" x2="12" y2="4"></line><line x1="6" y1="20" x2="6" y2="14"></line></svg>Reports</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-layers"><polygon points="12 2 2 7 12 12 22 7 12 2"></polygon><polyline points="2 17 12 22 22 17"></polyline><polyline points="2 12 12 17 22 12"></polyline></svg>Integrations</a></li></ul><h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted"><span>Saved reports</span><a class="d-flex align-items-center text-muted" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-plus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h6><ul class="nav flex-column mb-2"><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Current month</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Last quarter</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Social engagement</a></li><li class="nav-item"><a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-file-text"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline><line x1="16" y1="13" x2="8" y2="13"></line><line x1="16" y1="17" x2="8" y2="17"></line><polyline points="10 9 9 9 8 9"></polyline></svg>Year-end sale</a></li></ul></div>
</nav>
</body>
</html>

databasho.html

  • 给组件设置参数
div th:replace="~{commons/commons::silderbar(active='main.html')}"></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏 -->
<!-- 顶部导航栏   -->
<div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row">
<!-- 侧边栏看-->
<!--   设置插入语句,  ~{跟源html的名字:: th:fragment的名字 }           -->
<!--    传递参数给组件              --><div th:replace="~{commons/commons::silderbar(active='main.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><div class="chartjs-size-monitor" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;"><div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div></div><div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:200%;height:200%;left:0; top:0"></div></div></div><div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom"><h1 class="h2">Dashboard</h1><div class="btn-toolbar mb-2 mb-md-0"><div class="btn-group mr-2"><button class="btn btn-sm btn-outline-secondary">Share</button><button class="btn btn-sm btn-outline-secondary">Export</button></div><button class="btn btn-sm btn-outline-secondary dropdown-toggle"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-calendar"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect><line x1="16" y1="2" x2="16" y2="6"></line><line x1="8" y1="2" x2="8" y2="6"></line><line x1="3" y1="10" x2="21" y2="10"></line></svg>This week</button></div></div><canvas class="my-4 chartjs-render-monitor" id="myChart" width="1076" height="454" style="display: block; width: 1076px; height: 454px;"></canvas></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}" ></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}" ></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}" ></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}" ></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body>
</html>

list.html
传递参数

 <div th:replace="~{commons/commons::silderbar(active='list.html')}"></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏   --><div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar(active='list.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2>Section title</h2><div class="table-responsive"><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>data</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${list}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastName()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==1?'男':'女'}"></td><td th:text="${emp.getDepartment().getDepartmentName()}"></td><td th:text="${emp.getData()}"></td><td><button class="btn btn-sm btn-primary">编辑</button><button class="btn btn-sm btn-danger">删除</button></td></tr></tbody></table></div></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

(4).设置controller层

在controller层中,我们应该设置EmployeeDao进行自动装配。

package com.jsxs.config;import com.jsxs.dao.EmployeeDao;
import com.jsxs.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Collection;@Controller
public class EmployeeController {@Autowiredprivate EmployeeDao employeeDao;@RequestMapping("/emps")public String Employee(Model model){Collection<Employee> all = employeeDao.getAll();model.addAttribute("list",all);return "emp/list";}
}

(5).list.html进行遍历

thymeleaf进行遍历

<tr th:each="emp:${list}">
<div class="table-responsive"><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>data</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${list}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastName()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==1?'男':'女'}"></td><td th:text="${emp.getDepartment().getDepartmentName()}"></td><td th:text="${emp.getData()}"></td><td><button class="btn btn-sm btn-primary">编辑</button><button class="btn btn-sm btn-danger">删除</button></td></tr></tbody></table></div>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏   --><div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar(active='list.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2>Section title</h2><div class="table-responsive"><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>data</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${list}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastName()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==1?'男':'女'}"></td><td th:text="${emp.getDepartment().getDepartmentName()}"></td><td th:text="${emp.getData()}"></td><td><button class="btn btn-sm btn-primary">编辑</button><button class="btn btn-sm btn-danger">删除</button></td></tr></tbody></table></div></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

7.添加员工

(1).重定向注意事项

重定向的时候,我们的格式是死的,我们不能添加空格
错误演示

//    这里是进行实际的业务操作  post提交@PostMapping("/emp")public String add(){//        重定向的时候,格式是死的,不能随意添加空格,否则会报404页面return "redirect: /emps";}


正确演示

//    这里是进行实际的业务操作  post提交@PostMapping("/emp")public String add(){//        重定向的时候,格式是死的,不能随意添加空格,否则会报404页面return "redirect:/emps";}

(2).添加员工的页面

add.html
post提交

  • 我们的name属性都需要与实体类的属性名一致
  • 下拉框: 我们要设置成th:text,且th:vale并且select 这个标签中要设置成name= department.id
          <select class="form-control" name="department.id">
<!--           我们遍历获得到的部门数据,然后绑定text 为 DepartmentName ; value为Id --><option th:each="department:${departments}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}"></option></select>
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style>
</head><body>
<!-- 顶部导航栏   -->
<div th:replace="~{commons/commons::topbar}"></div>
<div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar(active='list.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<!--    在这里我们使用post的提交 所以我们可以起同样的名字  --><form th:action="@{/emp}" method="post"><div class="form-group"><label>LastName</label><input type="text" name="lastName" class="form-control" placeholder="吉士先生"></div><div class="form-group"><label>Email</label><input type="email" name="email" class="form-control" placeholder="2261203961@qq.com"></div><div class="form-group"><label>Gender</label><br><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="1"><label class="form-check-label">男</label></div><div class="form-check form-check-inline"><input class="form-check-input" type="radio" name="gender" value="0"><label class="form-check-label">女</label></div></div><div class="form-group"><label>department</label><!--我们在controller接受的是一个employee,所以我们需要提交的其中的一个属性--><select class="form-control" name="department.id">
<!--           我们遍历获得到的部门数据,然后绑定text 为 DepartmentName ; value为Id --><option th:each="department:${departments}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}"></option></select></div><div class="form-group"><label>Birth</label><input type="text" name="data" class="form-control" placeholder="2001/12/17"></div><button type="submit" class="btn btn-primary">添加</button></form></main></div>
</div><!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons -->
<script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script>
<script>feather.replace()
</script><!-- Graphs -->
<script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script>
<script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});
</script></body></html>

(3).controller层的实现

我们调用Emplyee里面的方法

package com.jsxs.controller;import com.jsxs.dao.DepartmentDao;
import com.jsxs.dao.EmployeeDao;
import com.jsxs.pojo.Department;
import com.jsxs.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Collection;@Controller
public class EmployeeController {@Autowiredprivate EmployeeDao employeeDao;@Autowiredprivate DepartmentDao departmentDao;@RequestMapping("/emps")public String Employee(Model model){Collection<Employee> all = employeeDao.getAll();model.addAttribute("list",all);return "emp/list";}//    这里是跳转页面  get提交@GetMapping("/emp")public String addEmployee(Model model){//        在我们跳转页面之前,我们要先查出所有的部门信息Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments",departments);return "/emp/add";}
//    这里是进行实际的业务操作  post提交@PostMapping("/emp")public String add(Employee employee){employeeDao.save(employee);System.out.println(employee);
//        重定向的时候,格式是死的,不能随意添加空格,否则会报404页面return "redirect:/emps";}
}

(4).解决疑惑

为什么我们只需要在下拉框中选择一个部门的名字就可以实现对部门类的添加?

  1. 因为我们th:text获取的是名字,然而th:value获取的是部门id.并且在select提交的时候的name值是遍历后对应的部门id。所以我们上传到后端的数据将会是一个id
  2. 又因为我们在employeeDao中设置了save()添加部门的方法。所以我们只需要提供一个id就会生成一个部门的对象。
/***  增加一个员工*/public void save(Employee employee){   //我们这里接受一个数据,她的信息是--》Employee 这个类if (employee.getId()==null){  //假如添加的员工id为空,那么我们就进行自增的操作employee.setId(initID++);}// 这里是我们为员工设置部门employee.setDepartment(departmentDao.getDepartmentByID(employee.getDepartment().getId()));  //我们这里调用了DepartmentDao,并且调用 通过id进行查找部门的方法//  这个是向数据库员工表中 添加员工信息employees.put(employee.getId(),employee);}

8.修改员工与删除员工

(1).update.html页面

(1).当我们点击对应栏目的修改的时候,我们需要在修改的页面自动弹出来对应的信息。(2).具体实现

  • 文本文框
 th:value="${employee.getLastName()}"
  • 单选框
th:checked="${employee.getGender()==1?true:false}"

*多选框

th:selected="${employee.getDepartment().getId()==department.getId()}"
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style>
</head><body>
<!-- 顶部导航栏   -->
<div th:replace="~{commons/commons::topbar}"></div>
<div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar(active='list.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><!--    在这里我们使用post的提交 所以我们可以起同样的名字  --><form th:action="@{/updateEmp}"><input type="hidden" name="id" th:value="${employee.getId()}"><div class="form-group"><label>LastName</label><input type="text" name="lastName" class="form-control" th:value="${employee.getLastName()}"></div><div class="form-group"><label>Email</label><input type="email" name="email" class="form-control" th:value="${employee.getEmail()}"></div><div class="form-group"><label>Gender</label><br><div class="form-check form-check-inline"><input th:checked="${employee.getGender()==1?true:false}" class="form-check-input" type="radio" name="gender" value="1"><label class="form-check-label">男</label></div><div class="form-check form-check-inline"><input th:checked="${employee.getGender()==0?true:false}" class="form-check-input" type="radio" name="gender" value="0"><label class="form-check-label">女</label></div></div><div class="form-group"><label>department</label><!--我们在controller接受的是一个employee,所以我们需要提交的其中的一个属性--><select class="form-control" name="department.id"><!--           我们遍历获得到的部门数据,然后绑定text 为 DepartmentName ; value为Id --><option th:each="department:${departments}" th:text="${department.getDepartmentName()}" th:value="${department.getId()}" th:selected="${employee.getDepartment().getId()==department.getId()}"></option></select></div><div class="form-group"><label>Birth</label><input type="text" name="data" class="form-control"  th:value="${#dates.format(employee.getData(),'yyyy-MM-dd HH:mm:ss')}"></div><button type="submit" class="btn btn-primary">修改</button></form></main></div>
</div><!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script>
<script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons -->
<script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script>
<script>feather.replace()
</script><!-- Graphs -->
<script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script>
<script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});
</script></body></html>

(2).list.html页面

  • 我们需要传递部门的id,目的是为了通过部门的id进行查找员工
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>Dashboard Template for Bootstrap</title><!-- Bootstrap core CSS --><link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet"><!-- Custom styles for this template --><link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet"><style type="text/css">/* Chart.js */@-webkit-keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}@keyframes chartjs-render-animation {from {opacity: 0.99}to {opacity: 1}}.chartjs-render-monitor {-webkit-animation: chartjs-render-animation 0.001s;animation: chartjs-render-animation 0.001s;}</style></head><body>
<!-- 顶部导航栏   --><div th:replace="~{commons/commons::topbar}"></div><div class="container-fluid"><div class="row"><!--   设置插入语句,  ~{被插入页面的名字:: th:fragment的名字 }            --><div th:replace="~{commons/commons::silderbar(active='list.html')}"></div><main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"><h2><a class="btn btn-sm btn-success" th:href="@{/emp}">添加员工</a></h2><div class="table-responsive"><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>data</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${list}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastName()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==1?'男':'女'}"></td><td th:text="${emp.getDepartment().getDepartmentName()}"></td><td th:text="${#dates.format(emp.getData(),'yyyy-MM-dd HH:mm:ss')}"></td><td>
<!--                     这里我们跳转的是映射 并不是html,所以不用添加后缀.html。我们得到的是部门的id            --><a class="btn btn-sm btn-primary" th:href="@{/update(id=${emp.getDepartment().getId()})}">编辑</a><a class="btn btn-sm btn-danger" th:href="@{/delete(id=${emp.getDepartment().getId()})}">删除</a></td></tr></tbody></table></div></main></div></div><!-- Bootstrap core JavaScript================================================== --><!-- Placed at the end of the document so the pages load faster --><script type="text/javascript" th:src="@{/asserts/js/jquery-3.2.1.slim.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/popper.min.js}"></script><script type="text/javascript" th:src="@{/asserts/js/bootstrap.min.js}"></script><!-- Icons --><script type="text/javascript" th:src="@{/asserts/js/feather.min.js}"></script><script>feather.replace()</script><!-- Graphs --><script type="text/javascript" th:src="@{/asserts/js/Chart.min.js}"></script><script>var ctx = document.getElementById("myChart");var myChart = new Chart(ctx, {type: 'line',data: {labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],datasets: [{data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],lineTension: 0,backgroundColor: 'transparent',borderColor: '#007bff',borderWidth: 4,pointBackgroundColor: '#007bff'}]},options: {scales: {yAxes: [{ticks: {beginAtZero: false}}]},legend: {display: false,}}});</script></body></html>

(3).controller业务层

package com.jsxs.controller;import com.jsxs.dao.DepartmentDao;
import com.jsxs.dao.EmployeeDao;
import com.jsxs.pojo.Department;
import com.jsxs.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Collection;@Controller
public class EmployeeController {@Autowiredprivate EmployeeDao employeeDao;@Autowiredprivate DepartmentDao departmentDao;@RequestMapping("/emps")public String Employee(Model model){Collection<Employee> all = employeeDao.getAll();model.addAttribute("list",all);return "emp/list";}//    这里是跳转页面  get提交@GetMapping("/emp")public String addEmployee(Model model){//        在我们跳转页面之前,我们要先查出所有的部门信息Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments",departments);return "/emp/add";}
//    这里是进行实际的业务操作  post提交@RequestMapping("/empAdd")public String add(Employee employee){employeeDao.save(employee);System.out.println(employee);
//        重定向的时候,格式是死的,不能随意添加空格,否则会报404页面return "redirect:/emps";}
//    修改前先进行跳转到对应的页面@GetMapping("/update")public String updateView(Integer id,Model model){System.out.println(id);Employee employee = employeeDao.getEmployeeByID(id);System.out.println(employee);Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments",departments);model.addAttribute("employee",employee);return "/emp/update";}
//    进行实现修改的业务@RequestMapping("/updateEmp")public String update(Employee employee){//        进行添加的操作employeeDao.save(employee);System.out.println("dsdsdsdsdsd-----"+employee);return "redirect:/emps";}//    进行删除的业务@GetMapping("/delete")public String delete(Integer id){employeeDao.deleteById(id);return "redirect:/emps";}
}

88.【员工管理系统-springBoot】相关推荐

  1. [附源码]计算机毕业设计基于Java的员工管理系统Springboot程序

    项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclis ...

  2. 员工管理系统---SpringBoot

    目录结构 全部代码 package com.kuang.config;import org.springframework.web.servlet.HandlerInterceptor;import ...

  3. Java-SpringBoot:员工管理系统

    Java:SpringBoot-员工管理系统 参考的代码和资源链接:https://blog.csdn.net/qq_45173404/article/details/108934414?spm=10 ...

  4. Springboot企业员工管理系统52y0w计算机毕业设计-课程设计-期末作业-毕设程序代做

    Springboot企业员工管理系统52y0w计算机毕业设计-课程设计-期末作业-毕设程序代做 [免费赠送源码]Springboot企业员工管理系统52y0w计算机毕业设计-课程设计-期末作业-毕设程 ...

  5. 基于springboot+thymeleaf+mybatis的员工管理系统 —— 增删改查

    员工管理系统 - 增删改查 entity 查询所有功能 查询所有的页面 emplist.html 保存员工 保存员工的页面 addEmp.html 删除员工 修改员工 根据id查询员工 修改员工信息 ...

  6. 基于springboot+thymeleaf+mybatis的员工管理系统 —— 登录与注册

    员工管理系统 - 登录与注册功能 创建项目 pom.xml 数据库表设计和环境准备 建表SQL application.properties 用户注册与登录功能 entity dao service ...

  7. springBoot员工管理系统

    员工管理系统 1.准备工作 静态资源:链接:https://pan.baidu.com/s/1qtUDuJNVupr872kVDO-veg 提取码:fabo gitee:https://gitee.c ...

  8. SpringBoot员工管理系统(整合Mybatis+mysql)

    SpringBoot员工管理系统(整合Mybatis+mysql) 前部分:https://blog.csdn.net/weixin_43501359/article/details/11271466 ...

  9. 计算机毕业设计之java+springboot基于vue的人事管理系统-员工管理系统

    计算机毕业设计之java+springboot基于vue的人事管理系统-员工管理系统 项目介绍 系统权限按管理员和员工这两类涉及用户划分. (a)管理员:管理员使用本系统涉到的功能主要有:首页,个人中 ...

最新文章

  1. 4.9.3 方法注释
  2. layui upload 额外参数上传
  3. 20个必会的JavaScript面试题
  4. Spring Cloud Alibaba - 24 Gateway-路由、断言(Predicate)、过滤器(Filter)初体验
  5. VTK:绘图之ChartMatrix
  6. G6 图可视化引擎——入门教程——图的交互 Behavior
  7. php require vs. include
  8. BWAI学习记录003_使用Chaoslauncher和AI(Stardust)人机对战
  9. ATMEL芯片系列介绍0
  10. Cocos Creator 随笔(1)-- 世界坐标,相对坐标转换问题
  11. joycdr2html.exe系统错误,win7安装软件CDR2020 弹出错误1719无法访问Windws Installer服务解决方案...
  12. 面向对象程序设计(Java)实验
  13. 蓝桥杯python-找单词出现次数最多的字母
  14. C++ winpcap网络抓包代码实现,以及抓包内容解析。
  15. lisp和python_给Lisp程序员的Python简介
  16. Visual Studio 2022的下载安装及使用
  17. 低功耗多通道WFAS1431无线数据采集采发仪使用流程说明
  18. python--石头剪刀布游戏(列表)
  19. *.zip.001 *.zip.002类型的文件的解压缩方法 即如何合并多个被分割的文件
  20. 博图os更新_西门子PLC固件如何操作更新?

热门文章

  1. SAP HANA 学习指南
  2. 如何实现浏览器的全屏效果
  3. 解决使用innobackupex备份mysql产生returned OS error 124
  4. 接口大师,即刻构建你的OpenAPI+开放平台
  5. python程序员搞笑段子_程序员的爆笑漫画和段子
  6. 硬件工程师的“真实写照”~
  7. 内存错误检测工具AddressSanitizer原理
  8. eXact 手动输入Lab数值
  9. Mapper 文件中SQL不等于的写法
  10. 车联网SOMEIP应用总结