• (狂神)员工管理系统项目


    目录

    ⭐最终代码

    一、引入static静态资源

     ⚪引入lombok依赖

    二、创建实体类

    1.department

    2.employee

    三、创建Dao层(模拟数据)

    1.DepartmentDao  

    2.EmployeeDao

    四、访问首页

    方式一:

    方式二:

    ⚪在application.properties中关闭模板引擎的缓存 

    😵注意!

    五、页面国际化(中英文切换)

    1.安装插件,实现可视化配置

    2.配置文件的真实位置

    3.在html代码中进行使用 

    4.最终效果

    ⚪总结

    1.我们如果需要在项目中进行按钮自动切换,需要自定义一个LocalResolver

    2.记得将写的组件配置到spring容器中(加上@Bean)  

    六、登录功能

    1.创建控制层

    2.在index.html中引入

    七、登录拦截器

    八、展示员工列表

    1.提取公共页面

    2.控制层

    3.列表循环展示

    九、添加功能

    1.提交按钮

    2.跳转到添加页面

    ①控制层

    ②add页面

    3.添加成功

    ⚪时间日期格式化

    4.返回首页

    十、修改功能

    1.修改页面

    2.跳转&控制层

    3. 最终效果

    十一、删除功能

    1.删除

    2.控制层

    3.最终效果 

    ⚪注销


    ⭐最终代码

    (狂神)员工管理系统项目(SpringBoot)-Java文档类资源-CSDN文库

    一、引入static静态资源

    狂神SpringBoot静态资源文件_RONG LU.的博客-CSDN博客_狂神springboot静态资源

     ⚪引入lombok依赖

    1. <dependency>
    2. <groupId>org.projectlombokgroupId>
    3. <artifactId>lombokartifactId>
    4. dependency>

    二、创建实体类

    1.department

    1. //部门表
    2. @Data
    3. @AllArgsConstructor
    4. @NoArgsConstructor
    5. public class Department {
    6. private Integer id;
    7. private String departmentName;
    8. }

    2.employee

    1. @Data
    2. @AllArgsConstructor //有参构造
    3. @NoArgsConstructor //无参构造
    4. public class Employee {
    5. private Integer id;
    6. private String lastName;
    7. private String email;
    8. private Integer gender;//0:女 1:男
    9. private Department department;
    10. private Date birth;
    11. public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
    12. this.id = id;
    13. this.lastName = lastName;
    14. this.email = email;
    15. this.gender = gender;
    16. this.department = department;
    17. //默认创建日期
    18. this.birth = new Date();
    19. }
    20. }

    三、创建Dao层(模拟数据)

    1.DepartmentDao  

    1. //部门dao
    2. @Repository
    3. public class DepartmentDao {
    4. //模拟数据库中的数据
    5. private static Map departments = null;
    6. static {
    7. //创建一个部门表
    8. departments = new HashMap();
    9. departments.put(101,new Department(101,"前端"));
    10. departments.put(102,new Department(102,"后台"));
    11. departments.put(103,new Department(103,"测试"));
    12. departments.put(104,new Department(104,"运维"));
    13. departments.put(105,new Department(105,"项目经理"));
    14. }
    15. //获得所有部门信息
    16. public Collection getDepartments(){
    17. return departments.values();
    18. }
    19. //通过id得到部门
    20. public Department getDepartmentById(Integer id){
    21. return departments.get(id);
    22. }
    23. }

    2.EmployeeDao

    1. @Repository
    2. public class EmployeeDao {
    3. //模拟数据库中的数据
    4. private static Map employees = null;
    5. //员工有所属部门
    6. @Autowired
    7. private DepartmentDao departmentDao;
    8. static{
    9. employees = new HashMap();
    10. employees.put(1001,new Employee(1001,"aa","741852963@qq.com",1,new Department(101,"前端")));
    11. employees.put(1002,new Employee(1002,"bb","963852741@qq.com",0,new Department(102,"后台")));
    12. employees.put(1003,new Employee(1003,"cc","123685748@qq.com",1,new Department(103,"测试")));
    13. employees.put(1004,new Employee(1004,"dd","987321654@qq.com",0,new Department(104,"运维")));
    14. employees.put(1005,new Employee(1005,"ee","951753852@qq.com",1,new Department(105,"项目经理")));
    15. }
    16. //主键自增
    17. private static Integer initId = 1006;
    18. //增加一个员工
    19. public void save(Employee employee){
    20. if (employee.getId() == null){
    21. employee.setId(initId++);
    22. }
    23. employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
    24. employees.put(employee.getId(),employee);
    25. }
    26. //查询全部员工信息
    27. public Collection getAll(){
    28. return employees.values();
    29. }
    30. //通过id查询员工
    31. public Employee getEmployeeById(Integer id){
    32. return employees.get(id);
    33. }
    34. //删除员工
    35. public void delete(Integer id){
    36. employees.remove(id);
    37. }
    38. }

    四、访问首页

    方式一:

    1. @Controller
    2. public class IndexController {
    3. @RequestMapping({"/","/index.html"})
    4. public String index(){
    5. return "index";
    6. }
    7. }

    方式二:

    1. @Configuration
    2. public class MyMvcConfig implements WebMvcConfigurer {
    3. @Override
    4. public void addViewControllers(ViewControllerRegistry registry) {
    5. registry.addViewController("/").setViewName("index");
    6. registry.addViewController("/index.html").setViewName("index");
    7. }
    8. }

    ⚪在application.properties中关闭模板引擎的缓存 

    1. #关闭模板引擎的缓存
    2. spring.thymeleaf.cache=false

    😵注意!

    所有页面的静态资源都需要使用thymeleaf接管  @{}         

    五、页面国际化(中英文切换)

    1.安装插件,实现可视化配置

    此时properties文件中的内容:

    2.配置文件的真实位置

    3.在html代码中进行使用 

    4.最终效果

    ⚪总结

    1.我们如果需要在项目中进行按钮自动切换,需要自定义一个LocalResolver

    1. public class MyLocalResolver implements LocaleResolver {
    2. //解析请求
    3. @Override
    4. public Locale resolveLocale(HttpServletRequest request) {
    5. //获取请求中的语言参数
    6. String language = request.getParameter("l");
    7. // 如果没有就使用默认的
    8. Locale locale = Locale.getDefault();
    9. //如果请求的链接携带了地区化的参数
    10. if (!StringUtils.isEmpty(language)){
    11. //zh_CN
    12. String[] split = language.split("_");
    13. //国家、地区
    14. locale = new Locale(split[0], split[1]);
    15. }
    16. return locale;
    17. }
    18. @Override
    19. public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    20. }
    21. }

    2.记得将写的组件配置到spring容器中(加上@Bean)  

    1. @Configuration
    2. public class MyMvcConfig implements WebMvcConfigurer {
    3. @Override
    4. public void addViewControllers(ViewControllerRegistry registry) {
    5. registry.addViewController("/").setViewName("index");
    6. registry.addViewController("/index.html").setViewName("index");
    7. }
    8. //自定义的国际化组件生效
    9. @Bean
    10. public LocaleResolver localeResolver(){
    11. return new MyLocalResolver();
    12. }
    13. }

    六、登录功能

    1.创建控制层

    1. @Controller
    2. public class LoginController {
    3. @RequestMapping("/user/login")
    4. public String login(
    5. @RequestParam("username") String username,
    6. @RequestParam("password") String password,
    7. Model model){
    8. //具体的业务
    9. if(!StringUtils.isEmpty(username) && "123456".equals(password)){
    10. return "redirect:/main.html";
    11. }else {
    12. //登陆失败
    13. model.addAttribute("msg","用户名或密码错误");
    14. return "index";
    15. }
    16. }
    17. }

    2.在index.html中引入

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    6. <meta name="description" content="">
    7. <meta name="author" content="">
    8. <title>Signin Template for Bootstraptitle>
    9. <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    10. <link th:href="@{/css/signin.css}" rel="stylesheet">
    11. head>
    12. <body class="text-center">
    13. <form class="form-signin" th:action="@{/user/login}">
    14. <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
    15. <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign inh1>
    16. <label class="sr-only" th:text="#{login.username}">Usernamelabel>
    17. <P style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">P>
    18. <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    19. <label class="sr-only" th:text="#{login.password}">Passwordlabel>
    20. <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
    21. <div class="checkbox mb-3">
    22. <label>
    23. <input type="checkbox" value="remember-me" th:text="#{login.remember}">
    24. label>
    25. div>
    26. <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign inbutton>
    27. <p class="mt-5 mb-3 text-muted">© 2022-2023p>
    28. <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文a>
    29. <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">Englisha>
    30. form>
    31. body>
    32. html>

      

    七、登录拦截器

    1. public class LoginHandlerInterceptor implements HandlerInterceptor {
    2. @Override
    3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    4. //登录成功之后,应该有用户的session
    5. Object loginUser = request.getSession().getAttribute("loginUser");
    6. if (loginUser == null){
    7. //没有登录
    8. request.setAttribute("msg","没有权限,请先登录!");
    9. request.getRequestDispatcher("/index.html").forward(request,response);
    10. return false;
    11. }else {
    12. return true;
    13. }
    14. }
    15. }

    在控制层中添加session 

    1. @Configuration
    2. public class MyMvcConfig implements WebMvcConfigurer {
    3. @Override
    4. public void addViewControllers(ViewControllerRegistry registry) {
    5. registry.addViewController("/").setViewName("index");
    6. registry.addViewController("/index.html").setViewName("index");
    7. registry.addViewController("/main.html").setViewName("dashboard");
    8. }
    9. //自定义的国际化组件生效
    10. @Bean
    11. public LocaleResolver localeResolver(){
    12. return new MyLocalResolver();
    13. }
    14. @Override
    15. public void addInterceptors(InterceptorRegistry registry) {
    16. registry.addInterceptor(new LoginHandlerInterceptor())
    17. .addPathPatterns("/**")
    18. .excludePathPatterns("/index.html","/","/user/login","/css/*","/js/**","/img/**");
    19. // .excludePathPatterns("/index.html","/","/user/login","/static/**");
    20. }
    21. }

    八、展示员工列表

    1.提取公共页面

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar">
    4. <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#" th:text="${session.loginUser}">a>
    5. <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
    6. <ul class="navbar-nav px-3">
    7. <li class="nav-item text-nowrap">
    8. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">注销a>
    9. li>
    10. ul>
    11. nav>
    12. <nav class="col-md-2 d-none d-md-block bg-light sidebar" th:fragment="sidebar">
    13. <div class="sidebar-sticky">
    14. <ul class="nav flex-column">
    15. <li class="nav-item">
    16. <a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/index.html}">
    17. <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">
    18. <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z">path>
    19. <polyline points="9 22 9 12 15 12 15 22">polyline>
    20. svg>
    21. 首页 <span class="sr-only">(current)span>
    22. a>
    23. li>
    24. <li class="nav-item">
    25. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    26. <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">
    27. <path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z">path>
    28. <polyline points="13 2 13 9 20 9">polyline>
    29. svg>
    30. Orders
    31. a>
    32. li>
    33. <li class="nav-item">
    34. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    35. <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">
    36. <circle cx="9" cy="21" r="1">circle>
    37. <circle cx="20" cy="21" r="1">circle>
    38. <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6">path>
    39. svg>
    40. Products
    41. a>
    42. li>
    43. <li class="nav-item">
    44. <a th:class="${active=='list.html'?'nav-link active':'nav-link'}" th:href="@{/emps}">
    45. 员工管理
    46. a>
    47. li>
    48. <li class="nav-item">
    49. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    50. <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">
    51. <line x1="18" y1="20" x2="18" y2="10">line>
    52. <line x1="12" y1="20" x2="12" y2="4">line>
    53. <line x1="6" y1="20" x2="6" y2="14">line>
    54. svg>
    55. Reports
    56. a>
    57. li>
    58. <li class="nav-item">
    59. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    60. <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">
    61. <polygon points="12 2 2 7 12 12 22 7 12 2">polygon>
    62. <polyline points="2 17 12 22 22 17">polyline>
    63. <polyline points="2 12 12 17 22 12">polyline>
    64. svg>
    65. Integrations
    66. a>
    67. li>
    68. ul>
    69. <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
    70. <span>Saved reportsspan>
    71. <a class="d-flex align-items-center text-muted" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    72. <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>
    73. a>
    74. h6>
    75. <ul class="nav flex-column mb-2">
    76. <li class="nav-item">
    77. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    78. <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">
    79. <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z">path>
    80. <polyline points="14 2 14 8 20 8">polyline>
    81. <line x1="16" y1="13" x2="8" y2="13">line>
    82. <line x1="16" y1="17" x2="8" y2="17">line>
    83. <polyline points="10 9 9 9 8 9">polyline>
    84. svg>
    85. Current month
    86. a>
    87. li>
    88. <li class="nav-item">
    89. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    90. <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">
    91. <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z">path>
    92. <polyline points="14 2 14 8 20 8">polyline>
    93. <line x1="16" y1="13" x2="8" y2="13">line>
    94. <line x1="16" y1="17" x2="8" y2="17">line>
    95. <polyline points="10 9 9 9 8 9">polyline>
    96. svg>
    97. Last quarter
    98. a>
    99. li>
    100. <li class="nav-item">
    101. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    102. <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">
    103. <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z">path>
    104. <polyline points="14 2 14 8 20 8">polyline>
    105. <line x1="16" y1="13" x2="8" y2="13">line>
    106. <line x1="16" y1="17" x2="8" y2="17">line>
    107. <polyline points="10 9 9 9 8 9">polyline>
    108. svg>
    109. Social engagement
    110. a>
    111. li>
    112. <li class="nav-item">
    113. <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
    114. <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">
    115. <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z">path>
    116. <polyline points="14 2 14 8 20 8">polyline>
    117. <line x1="16" y1="13" x2="8" y2="13">line>
    118. <line x1="16" y1="17" x2="8" y2="17">line>
    119. <polyline points="10 9 9 9 8 9">polyline>
    120. svg>
    121. Year-end sale
    122. a>
    123. li>
    124. ul>
    125. div>
    126. nav>
    127. html>

    2.控制层

    1. @Controller
    2. public class EmployeeController {
    3. //查询所有的员工
    4. @Autowired
    5. EmployeeDao employeeDao;
    6. @RequestMapping("/emps")
    7. public String list(Model model){
    8. Collection employees = employeeDao.getAll();
    9. model.addAttribute("emps",employees);
    10. return "emp/list";
    11. }
    12. }

     使用()传递参数,完成接收判断

    3.列表循环展示

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    6. <meta name="description" content="">
    7. <meta name="author" content="">
    8. <title>Dashboard Template for Bootstraptitle>
    9. <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    10. <link th:href="@{/css/dashboard.css}" rel="stylesheet">
    11. <style type="text/css">
    12. /* Chart.js */
    13. @-webkit-keyframes chartjs-render-animation {
    14. from {
    15. opacity: 0.99
    16. }
    17. to {
    18. opacity: 1
    19. }
    20. }
    21. @keyframes chartjs-render-animation {
    22. from {
    23. opacity: 0.99
    24. }
    25. to {
    26. opacity: 1
    27. }
    28. }
    29. .chartjs-render-monitor {
    30. -webkit-animation: chartjs-render-animation 0.001s;
    31. animation: chartjs-render-animation 0.001s;
    32. }
    33. style>
    34. head>
    35. <body>
    36. <div th:replace="~{commons/commons::topbar}">div>
    37. <div class="container-fluid">
    38. <div class="row">
    39. <div th:replace="~{commons/commons::sidebar(active='list.html')}">div>
    40. <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    41. <h2>Section titleh2>
    42. <div class="table-responsive">
    43. <table class="table table-striped table-sm">
    44. <thead>
    45. <tr>
    46. <th>idth>
    47. <th>lastNameth>
    48. <th>emailth>
    49. <th>genderth>
    50. <th>departmentth>
    51. <th>birthth>
    52. <th>操作th>
    53. tr>
    54. thead>
    55. <tbody>
    56. <tr th:each="emp:${emps}">
    57. <td th:text="${emp.getId()}">td>
    58. <td th:text="${emp.getLastName()}">td>
    59. <td th:text="${emp.getEmail()}">td>
    60. <td th:text="${emp.getGender()==0?'女':'男'}">td>
    61. <td th:text="${emp.department.getDepartmentName()}">td>
    62. <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}">td>
    63. <td>
    64. <button class="btn btn-sm btn-primary">编辑button>
    65. <button class="btn btn-sm btn-danger">删除button>
    66. td>
    67. tr>
    68. tbody>
    69. table>
    70. div>
    71. main>
    72. div>
    73. div>
    74. <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js">script>
    75. <script type="text/javascript" src="asserts/js/popper.min.js">script>
    76. <script type="text/javascript" src="asserts/js/bootstrap.min.js">script>
    77. <script type="text/javascript" src="asserts/js/feather.min.js">script>
    78. <script>
    79. feather.replace()
    80. script>
    81. <script type="text/javascript" src="asserts/js/Chart.min.js">script>
    82. <script>
    83. var ctx = document.getElementById("myChart");
    84. var myChart = new Chart(ctx, {
    85. type: 'line',
    86. data: {
    87. labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    88. datasets: [{
    89. data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
    90. lineTension: 0,
    91. backgroundColor: 'transparent',
    92. borderColor: '#007bff',
    93. borderWidth: 4,
    94. pointBackgroundColor: '#007bff'
    95. }]
    96. },
    97. options: {
    98. scales: {
    99. yAxes: [{
    100. ticks: {
    101. beginAtZero: false
    102. }
    103. }]
    104. },
    105. legend: {
    106. display: false,
    107. }
    108. }
    109. });
    110. script>
    111. body>
    112. html>

    九、添加功能

    1.提交按钮

    2.跳转到添加页面

    ①控制层

    1. @GetMapping("/emp")
    2. public String toAddPage(Model model){
    3. //查询所有部门的信息
    4. Collection departments = departmentDao.getDepartments();
    5. model.addAttribute("departments",departments);
    6. return "emp/add";
    7. }
    8. @PostMapping("/emp")
    9. public String addEmp(Employee employee){
    10. //添加操作,调用底层业务方法
    11. employeeDao.save(employee);
    12. return "redirect:/emps";
    13. }

    ②add页面

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    6. <meta name="description" content="">
    7. <meta name="author" content="">
    8. <title>Dashboard Template for Bootstraptitle>
    9. <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    10. <link th:href="@{/css/dashboard.css}" rel="stylesheet">
    11. <style type="text/css">
    12. /* Chart.js */
    13. @-webkit-keyframes chartjs-render-animation {
    14. from {
    15. opacity: 0.99
    16. }
    17. to {
    18. opacity: 1
    19. }
    20. }
    21. @keyframes chartjs-render-animation {
    22. from {
    23. opacity: 0.99
    24. }
    25. to {
    26. opacity: 1
    27. }
    28. }
    29. .chartjs-render-monitor {
    30. -webkit-animation: chartjs-render-animation 0.001s;
    31. animation: chartjs-render-animation 0.001s;
    32. }
    33. style>
    34. head>
    35. <body>
    36. <div th:replace="~{commons/commons::topbar}">div>
    37. <div class="container-fluid">
    38. <div class="row">
    39. <div th:replace="~{commons/commons::sidebar(active='list.html')}">div>
    40. <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    41. <form th:action="@{/emp}" method="post">
    42. <div class="form-group">
    43. <label>LastNamelabel>
    44. <input type="text" name="lastName" class="form-control" placeholder="海绵宝宝">
    45. div>
    46. <div class="form-group">
    47. <label>Emaillabel>
    48. <input type="email" name="email" class="form-control" placeholder="1234567890@qq.com">
    49. div>
    50. <div class="form-group">
    51. <label>Genderlabel><br>
    52. <div class="form-check form-check-inline">
    53. <input class="form-check-input" type="radio" name="gender" value="1">
    54. <label class="form-check-label">label>
    55. div>
    56. <div class="form-check form-check-inline">
    57. <input class="form-check-input" type="radio" name="gender" value="0">
    58. <label class="form-check-label">label>
    59. div>
    60. div>
    61. <div class="form-group">
    62. <label>departmentlabel>
    63. <select class="form-control" name="department.id">
    64. <option th:each="dept:${departments}"
    65. th:text="${dept.getDepartmentName()}"
    66. th:value="${dept.getId()}">option>
    67. select>
    68. div>
    69. <div class="form-group">
    70. <label>Birthlabel>
    71. <input type="text" name="birth" class="form-control" placeholder="yyyy-MM-dd">
    72. div>
    73. <button type="submit" class="btn btn-primary">添加button>
    74. form>
    75. main>
    76. div>
    77. div>
    78. <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js">script>
    79. <script type="text/javascript" src="asserts/js/popper.min.js">script>
    80. <script type="text/javascript" src="asserts/js/bootstrap.min.js">script>
    81. <script type="text/javascript" src="asserts/js/feather.min.js">script>
    82. <script>
    83. feather.replace()
    84. script>
    85. <script type="text/javascript" src="asserts/js/Chart.min.js">script>
    86. <script>
    87. var ctx = document.getElementById("myChart");
    88. var myChart = new Chart(ctx, {
    89. type: 'line',
    90. data: {
    91. labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    92. datasets: [{
    93. data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
    94. lineTension: 0,
    95. backgroundColor: 'transparent',
    96. borderColor: '#007bff',
    97. borderWidth: 4,
    98. pointBackgroundColor: '#007bff'
    99. }]
    100. },
    101. options: {
    102. scales: {
    103. yAxes: [{
    104. ticks: {
    105. beginAtZero: false
    106. }
    107. }]
    108. },
    109. legend: {
    110. display: false,
    111. }
    112. }
    113. });
    114. script>
    115. body>
    116. html>

    3.添加成功

    根据ThymeleafViewResolver中定义的forward和redirect规则,添加员工信息时不能使用forward!

    1. @Override
    2. protected View createView(final String viewName, final Locale locale) throws Exception {
    3. // First possible call to check "viewNames": before processing redirects and forwards
    4. if (!this.alwaysProcessRedirectAndForward && !canHandle(viewName, locale)) {
    5. vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
    6. return null;
    7. }
    8. // Process redirects (HTTP redirects)
    9. if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
    10. vrlogger.trace("[THYMELEAF] View \"{}\" is a redirect, and will not be handled directly by ThymeleafViewResolver.", viewName);
    11. final String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length(), viewName.length());
    12. final RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
    13. return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, REDIRECT_URL_PREFIX);
    14. }
    15. // Process forwards (to JSP resources)
    16. if (viewName.startsWith(FORWARD_URL_PREFIX)) {
    17. // The "forward:" prefix will actually create a Servlet/JSP view, and that's precisely its aim per the Spring
    18. // documentation. See http://docs.spring.io/spring-framework/docs/4.2.4.RELEASE/spring-framework-reference/html/mvc.html#mvc-redirecting-forward-prefix
    19. vrlogger.trace("[THYMELEAF] View \"{}\" is a forward, and will not be handled directly by ThymeleafViewResolver.", viewName);
    20. final String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length(), viewName.length());
    21. return new InternalResourceView(forwardUrl);
    22. }
    23. // Second possible call to check "viewNames": after processing redirects and forwards
    24. if (this.alwaysProcessRedirectAndForward && !canHandle(viewName, locale)) {
    25. vrlogger.trace("[THYMELEAF] View \"{}\" cannot be handled by ThymeleafViewResolver. Passing on to the next resolver in the chain.", viewName);
    26. return null;
    27. }
    28. vrlogger.trace("[THYMELEAF] View {} will be handled by ThymeleafViewResolver and a " +
    29. "{} instance will be created for it", viewName, getViewClass().getSimpleName());
    30. return loadView(viewName, locale);
    31. }

    ⚪时间日期格式化

             

    4.返回首页

    十、修改功能

    1.修改页面

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    6. <meta name="description" content="">
    7. <meta name="author" content="">
    8. <title>Dashboard Template for Bootstraptitle>
    9. <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    10. <link th:href="@{/css/dashboard.css}" rel="stylesheet">
    11. <style type="text/css">
    12. /* Chart.js */
    13. @-webkit-keyframes chartjs-render-animation {
    14. from {
    15. opacity: 0.99
    16. }
    17. to {
    18. opacity: 1
    19. }
    20. }
    21. @keyframes chartjs-render-animation {
    22. from {
    23. opacity: 0.99
    24. }
    25. to {
    26. opacity: 1
    27. }
    28. }
    29. .chartjs-render-monitor {
    30. -webkit-animation: chartjs-render-animation 0.001s;
    31. animation: chartjs-render-animation 0.001s;
    32. }
    33. style>
    34. head>
    35. <body>
    36. <div th:replace="~{commons/commons::topbar}">div>
    37. <div class="container-fluid">
    38. <div class="row">
    39. <div th:replace="~{commons/commons::sidebar(active='list.html')}">div>
    40. <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    41. <form th:action="@{/updateEmp}" method="post">
    42. <input type="hidden" name="id" th:value="${emp.getId()}">
    43. <div class="form-group">
    44. <label>LastNamelabel>
    45. <input th:value="${emp.getLastName()}" type="text" name="lastName" class="form-control" placeholder="海绵宝宝">
    46. div>
    47. <div class="form-group">
    48. <label>Emaillabel>
    49. <input th:value="${emp.getEmail()}" type="email" name="email" class="form-control" placeholder="1234567890@qq.com">
    50. div>
    51. <div class="form-group">
    52. <label>Genderlabel><br>
    53. <div class="form-check form-check-inline">
    54. <input th:checked="${emp.getGender()==1}" class="form-check-input" type="radio" name="gender" value="1">
    55. <label class="form-check-label">label>
    56. div>
    57. <div class="form-check form-check-inline">
    58. <input th:checked="${emp.getGender()==0}" class="form-check-input" type="radio" name="gender" value="0">
    59. <label class="form-check-label">label>
    60. div>
    61. div>
    62. <div class="form-group">
    63. <label>departmentlabel>
    64. <select class="form-control" name="department.id">
    65. <option th:selected="${dept.getId()==emp.getDepartment().getId()}"
    66. th:each="dept:${departments}"
    67. th:text="${dept.getDepartmentName()}"
    68. th:value="${dept.getId()}">option>
    69. select>
    70. div>
    71. <div class="form-group">
    72. <label>Birthlabel>
    73. <input th:value="${#dates.format(emp.getBirth(),'yyy-MM-dd HH:mm')}" type="text" name="birth" class="form-control" placeholder="yyyy-MM-dd">
    74. div>
    75. <button type="submit" class="btn btn-primary">修改button>
    76. form>
    77. main>
    78. div>
    79. div>
    80. <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js">script>
    81. <script type="text/javascript" src="asserts/js/popper.min.js">script>
    82. <script type="text/javascript" src="asserts/js/bootstrap.min.js">script>
    83. <script type="text/javascript" src="asserts/js/feather.min.js">script>
    84. <script>
    85. feather.replace()
    86. script>
    87. <script type="text/javascript" src="asserts/js/Chart.min.js">script>
    88. <script>
    89. var ctx = document.getElementById("myChart");
    90. var myChart = new Chart(ctx, {
    91. type: 'line',
    92. data: {
    93. labels: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    94. datasets: [{
    95. data: [15339, 21345, 18483, 24003, 23489, 24092, 12034],
    96. lineTension: 0,
    97. backgroundColor: 'transparent',
    98. borderColor: '#007bff',
    99. borderWidth: 4,
    100. pointBackgroundColor: '#007bff'
    101. }]
    102. },
    103. options: {
    104. scales: {
    105. yAxes: [{
    106. ticks: {
    107. beginAtZero: false
    108. }
    109. }]
    110. },
    111. legend: {
    112. display: false,
    113. }
    114. }
    115. });
    116. script>
    117. body>
    118. html>

    2.跳转&控制层

    <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.getId()}">编辑a>
    1. //跳转至修改页面
    2. @GetMapping("/emp/{id}")
    3. public String toUpdateEmp(@PathVariable("id") Integer id,Model model){
    4. //查询要修改的数据
    5. Employee employee = employeeDao.getEmployeeById(id);
    6. model.addAttribute("emp",employee);
    7. System.out.println(employee);
    8. //查出所有部门的信息
    9. Collection departments = departmentDao.getDepartments();
    10. model.addAttribute("departments",departments);
    11. return "emp/update";
    12. }
    13. @PostMapping("/updateEmp")
    14. public String updateEmp(Employee employee){
    15. employeeDao.save(employee);
    16. return "redirect:/emps";
    17. }

    3. 最终效果

    点击修改即可保存修改的信息

     

    十一、删除功能

    1.删除

    <a class="btn btn-sm btn-danger" th:href="@{/delemp/}+${emp.getId()}">删除a>

    2.控制层

    1. @GetMapping("/delemp/{id}")
    2. public String deleteEmp(@PathVariable("id") Integer id){
    3. employeeDao.delete(id);
    4. return "redirect:/emps";
    5. }

    3.最终效果 

    点击删除

    ⚪注销

    <a class="nav-link" th:href="@{/user/logout}">注销a>

    控制层 

    1. @RequestMapping("/user/logout")
    2. public String logout(HttpSession session){
    3. session.invalidate();
    4. return "redirect:/index.html";
    5. }

     

  • 相关阅读:
    ISP代理是什么?双ISP是什么意思?
    C++内存分类
    1444_TC275 DataSheet阅读笔记5_部分管脚功能的梳理
    c++ using用法之一
    22.0 Pycharm中编写js代码
    uniapp实现移动端的视频图片轮播组件
    Vue中使用Echarts封装为公用组件(简单复制粘贴)
    iPhone 15 或将换成 USB-C 接口?网友:Lightning 已经落后了
    数组:
    储油罐北斗监测方案
  • 原文地址:https://blog.csdn.net/m0_52982868/article/details/126538134