发表于: 2020-08-04 23:42:50

1 1409


今天完成的事:

使用springboot实现增删改查

新建一个springboot项目

导入相关依赖

<dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
<!--          jquery-->
       <dependency>
           <groupId>org.webjars</groupId>
           <artifactId>jquery</artifactId>
           <version>3.5.1</version>
       </dependency>
       <!--thymeleaf 基于3.xx-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
<!--         单元测试-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
           <exclusions>
               <exclusion>
                   <groupId>org.junit.vintage</groupId>
                   <artifactId>junit-vintage-engine</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>


编写两个实体类

public class Department {

private Integer id;
   private String departmentName;

   public Department() {
}

public Department(Integer id, String departmentName) {
this.id = id;
       this.departmentName = departmentName;
   }

public Integer getId() {
return id;
   }

public void setId(Integer id) {
this.id = id;
   }

public String getDepartmentName() {
return departmentName;
   }

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
   }

@Override
   public String toString() {
return "Department{" +
"id=" + id +
", departmentName='" + departmentName + '\'' +
'}';
   }
}


public class Employee {

private Integer id;
   private String lastName;

   private String email;
   //1 male,0 female
   private Integer gender;
   private Department department;
   private Date birth;

   public Employee() {
}

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

       //默认的创建日期
       this.birth = new Date();
   }

public Integer getId() {
return id;
   }

public void setId(Integer id) {
this.id = id;
   }

public String getLastName() {
return lastName;
   }

public void setLastName(String lastName) {
this.lastName = lastName;
   }

public String getEmail() {
return email;
   }

public void setEmail(String email) {
this.email = email;
   }

public Integer getGender() {
return gender;
   }

public void setGender(Integer gender) {
this.gender = gender;
   }

public Department getDepartment() {
return department;
   }

public void setDepartment(Department department) {
this.department = department;
   }

public Date getBirth() {
return birth;
   }

public void setBirth(Date birth) {
this.birth = birth;
   }

@Override
   public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", department=" + department +
", birth=" + birth +
'}';
   }

}


编写对应的dao层

这里将数据库模拟写在了dao层

//员工Dao
@Repository
public class EmployeeDao {

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

   @Autowired
   private DepartmentDao departmentDao;

   static {
employees = new HashMap<Integer, Employee>();

       employees.put(1001,new Employee(1001,"E-AA","aa@163.com",1,new Department(101,"D-AA")));
       employees.put(1002,new Employee(1002,"E-BB","bb@163.com",0,new Department(102,"D-BB")));
       employees.put(1003,new Employee(1003,"E-CC","cc@163.com",1,new Department(103,"D-CC")));
       employees.put(1004,new Employee(1004,"E-DD","dd@163.com",0,new Department(104,"D-DD")));
       employees.put(1005,new Employee(1005,"E-EE","ee@163.com",1,new Department(105,"D-EE")));

   }

//主键自增
   private static Integer initId = 1006;

   //
   public void save(Employee employee){
if(employee.getId()==null){
employee.setId(initId++);
       }

employee.setDepartment(departmentDao.getDepartmentById(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);
   }
}


//部门Dao
@Repository
public class DepartmentDao {

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


   static {
departments = new HashMap<>();
       departments.put(101,new Department(101,"D-AA"));
       departments.put(102,new Department(102,"D-BB"));
       departments.put(103,new Department(103,"D-CC"));
       departments.put(104,new Department(104,"D-DD"));
       departments.put(105,new Department(105,"D-EE"));
   }

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

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


导入相关的静态资源和html页面 具体结构如下

这里都是前端知识,就不赘述了


首页-登录页面

<!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 -->
     <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
     <!-- Custom styles for this template -->
     <link th:href="@{/css/signin.css}" rel="stylesheet">
  </head>

  <body class="text-center">
     <form class="form-signin" th:action="@{/user/login}">
        <img class="mb-4" th:src="@{/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的值为空则不显示消息-->
        <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
        <label class="sr-only" th:text="#{login.username}"></label>
        <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
        <label class="sr-only" th:text="#{login.password}"></label>
        <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
        <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>

先写一个登录功能

@Controller
public class LoginController {

@RequestMapping("/user/login")
public String login(@RequestParam("username") String username,
                       @RequestParam("password") String password,
                       Map<String,Object> map, HttpSession session){
if (!StringUtils.isEmpty(username) && "123456".equals(password)){
//登录成功,防止表单重复提交,可以重定向到主页
           session.setAttribute("loginUser",username);;
           return "redirect:/main.html";
           //return "dashboard";
       }else {
//登录失败
           map.put("msg","用户名或密码错误");
           return "index";
       }
}
}


编写一个映射资源配置

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

@Override
   public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
       registry.addViewController("/index.html").setViewName("index");
       registry.addViewController("/main.html").setViewName("dashboard");
   }

//自定义的国际化组件生效
   @Bean
   public LocaleResolver localeResolver(){
return new MycaleResolver();
   }

@Override
   public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
       //静态资源;  *.css , *.js
       //SpringBoot已经做好了静态资源映射
       registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**").excludePathPatterns
("/index.html","/","/user/login","*.css","*.js");
   }
}


编写一个登录拦截器,用户必须登录才能进行页面访问

public class LoginHandlerInterceptor implements HandlerInterceptor {

@Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//登录成功之后,应该有用户的session
       Object loginUser = request.getSession().getAttribute("loginUser");

       if (loginUser==null){ //没有登录
           request.setAttribute("msg","没有权限,请先登录");
           request.getRequestDispatcher("index.html").forward(request,response);
           return false;
       }else {
return true;
       }
}
}



在controller中编写对员工增删改查

@Controller
public class EmployeeController {

@Autowired
   EmployeeDao employeeDao;

   @Autowired
   DepartmentDao departmentDao;

   //查询所有员工返回列表页面
   @RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees = employeeDao.getAll();

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

//来到员工添加页面
   @GetMapping("/emp")
public String toAddPage(Model model){
//来到添加页面,查出所有的部门,在页面显示
       Collection<Department> departments = departmentDao.getDepartments();
       model.addAttribute("depts",departments);
       return "emp/add";
   }

//员工添加
   //SpringMVC自动将请求参数和入参对象的属性进行一一绑定;要求请求参数的名字和JavaBean入参的对象里面的属性名是一样的
   @PostMapping("/emp")
public String addEmp(Employee employee){
//来到员工列表页面
       System.out.println("保存的员工信息:"+employee);
       //保存员工
       employeeDao.save(employee);
       //redirect:表示重定向到一个地址
       //forward:表示转发到一个地址
       return "redirect:/emps";
   }

//来到修改页面,查出当前员工,在页面回显
   @GetMapping("/emp/{id}")
public String toEditPage(@PathVariable("id") Integer id,Model model){
Employee employee = employeeDao.getEmployeeById(id);
       model.addAttribute("emp",employee);

       //页面要显示所有的部门列表
       Collection<Department> departments = departmentDao.getDepartments();
       model.addAttribute("departments",departments);
       //回到修改页面(add是一个修改添加二合一的页面)
       return "emp/update";
   }

//员工修改:需要提交员工id
   @PutMapping("/emp")
public String updateEmployee(Employee employee){
System.out.println("修改的员工数据:"+employee);
       employeeDao.save(employee);
       return "redirect:/emps";
   }

//员工删除
   @GetMapping("/delemp/{id}")
public String deleteEmployee(@PathVariable("id") Integer id){
employeeDao.delete(id);
       return "redirect:/emps";
   }

//注销
   @RequestMapping("/user/logout")
public String UserLogout(HttpSession httpSession){
httpSession.invalidate();
       return "redirect:/index.html";
   }
}


前端主要的页面

dashboard页面

<!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="@{/css/bootstrap.min.css}" rel="stylesheet">

     <!-- Custom styles for this template -->
     <link th:href="@{/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">
           <!--侧边栏-->
           <div th:replace="~{commons/commons::sidebar}"></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" src="/js/jquery-3.2.1.slim.min.js" ></script>
     <script type="text/javascript" src="/js/popper.min.js" ></script>
     <script type="text/javascript" src="/js/bootstrap.min.js" ></script>

     <!-- Icons -->
     <script type="text/javascript" src="/js/feather.min.js" ></script>
     <script>
        feather.replace()
</script>

     <!-- Graphs -->
     <script type="text/javascript" src="/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

<!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="@{/css/bootstrap.min.css}" rel="stylesheet">

     <!-- Custom styles for this template -->
     <link th:href="@{/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">
           <div th:insert="~{commons/commons::sidebar}"></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>编号</th>
                          <th>名字</th>
                          <th>邮箱</th>
                          <th>性别</th>
                          <th>部门</th>
                          <th>出身日期</th>
                          <th>操作</th>
                       </tr>
                    </thead>
                    <tbody>
                       <tr th:each="emp:${emps}">
                          <td th:text="${emp.getId()}"/>
                          <td>[[${emp.getLastName()}]]</td>
                          <td th:text="${emp.getEmail()}"/>
                          <td th:text="${emp.getGender()==0? '':''}"/>
                          <!--emp.department.getDepartmentNam 1e()-->
                          <td th:text="${emp.department.getDepartmentName()}"/>
                          <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"/>
                          <td>
                             <a class="btn btn-sm btn-primary" th:href="@{'/emp/'+${emp.getId()}}">编辑</a>
                             <a class="btn btn-sm btn-danger" th:href="@{'/delemp/'+${emp.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" src="/js/jquery-3.2.1.slim.min.js"></script>
     <script type="text/javascript" src="/js/popper.min.js"></script>
     <script type="text/javascript" src="/js/bootstrap.min.js"></script>

     <!-- Icons -->
     <script type="text/javascript" src="/js/feather.min.js"></script>
     <script>
        feather.replace()
</script>

     <!-- Graphs -->
     <script type="text/javascript" src="/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>


add.html

<!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>Dashboard Template for Bootstrap</title>
     <!-- Bootstrap core CSS -->
     <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

     <!-- Custom styles for this template -->
     <link th:href="@{/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">
           <div th:insert="~{commons/commons::sidebar}"></div>

           <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
              <form th:action="@{/emp}" method="post">
                 <div class="form-group">
                    <label>姓名</label>
                    <input type="text" name="lastName" class="form-control" placeholder="danjiayi">
                 </div>
                 <div class="form-group">
                    <label>邮箱</label>
                    <input type="email" name="email" class="form-control" placeholder="222@163.com">
                 </div>
                 <div class="form-group">
                    <label>性别</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>部门选择</label>
                    <select class="form-control" th:name="department.id">
                       <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"/>
                    </select>-->
                    <label>部门选择</label>
                    <select class="form-control" name="department.id">
                       <option
                          th:each="dept:${depts}"
                          th:text="${dept.getDepartmentName()}"
                          th:value="${dept.getId()}">
                       </option>
                    </select>
                 </div>
                 <div class="form-group">
                    <label>出生日期</label>
                    <input type="text" name="birth" class="form-control" placeholder="yyyy-MM-dd">
                 </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" src="/js/jquery-3.2.1.slim.min.js"></script>
     <script type="text/javascript" src="/js/popper.min.js"></script>
     <script type="text/javascript" src="/js/bootstrap.min.js"></script>

     <!-- Icons -->
     <script type="text/javascript" src="/js/feather.min.js"></script>
     <script>
        feather.replace()
</script>

     <!-- Graphs -->
     <script type="text/javascript" src="/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>


update.html

<!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>Dashboard Template for Bootstrap</title>
     <!-- Bootstrap core CSS -->
     <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

     <!-- Custom styles for this template -->
     <link th:href="@{/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">
           <div th:insert="~{commons/commons::sidebar}"></div>

           <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
              <form th:action="@{/emp}" method="post">
<!--                  id隐藏域-->
                 <input type="hidden" th:value="${emp.getId()}" name="id">
                 <div class="form-group">
                    <label>姓名</label>
                    <input th:value="${emp.getLastName()}" type="text" name="lastName" class="form-control" placeholder="danjiayi">
                 </div>
                 <div class="form-group">
                    <label>邮箱</label>
                    <input th:value="${emp.getEmail()}" type="email" name="email" class="form-control" placeholder="222@163.com">
                 </div>
                 <div class="form-group">
                    <label>性别</label><br/>
                    <div class="form-check form-check-inline">
                       <input th:checked="${emp.getGender()==1}" 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="${emp.getGender()==0}" class="form-check-input" type="radio" name="gender" value="0">
                       <label class="form-check-label"></label>
                    </div>
                 </div>
                 <div class="form-group">
<!--                     <label>部门选择</label>-->
<!--                     <select class="form-control" th:name="department.id">-->
<!--                        <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}"-->
<!--                              th:value="${dept.getId()}"/>-->
<!--                     </select>-->
                    <label>部门选择</label>
                    <select class="form-control" name="department.id">
                       <option th:selected="${depts.getId()==emp.getDepartment().getId()}"
                          th:each="depts:${departments}"
                          th:text="${depts.getDepartmentName()}"
                          th:value="${depts.getId()}">
                       </option>
                    </select>
                 </div>
                 <div class="form-group">
                    <label>出生日期</label>
                    <input th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}" type="text" name="birth" class="form-control" placeholder="yyyy-MM-dd">
                 </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" src="/js/jquery-3.2.1.slim.min.js"></script>
     <script type="text/javascript" src="/js/popper.min.js"></script>
     <script type="text/javascript" src="/js/bootstrap.min.js"></script>

     <!-- Icons -->
     <script type="text/javascript" src="/js/feather.min.js"></script>
     <script>
        feather.replace()
</script>

     <!-- Graphs -->
     <script type="text/javascript" src="/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>


commons.html

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

<!--topbar-->
<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="搜索查询" aria-label="Search">
   <ul class="navbar-nav px-3">
       <li class="nav-item text-nowrap">
           <a class="nav-link" th:href="@{/user/logout}">注销</a>
       </li>
   </ul>
</nav>

<!--sidebar-->
<nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="sidebar">
   <div class="sidebar-sticky">
       <ul class="nav flex-column">
           <li class="nav-item">
               <a class="nav-link active" 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>
                   订单
               </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>
                   产品展示
               </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>
                   报告书
               </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>
                   整合方式
               </a>
           </li>
       </ul>

       <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
           <span>保存的报告</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>
                   这个月
               </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>
                   上个季度
               </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>
                   社会参与
               </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>
                   年终大减价
               </a>
           </li>
       </ul>
   </div>
</nav>

</html>


这里还用到了thyemleaf模板的页面拼接,主要都是前端的知识

这里展示一下主要流程,暂时也只是看的懂的


明天计划的事:

遇到的困难:

收获:



返回列表 返回列表
评论

    分享到