【WEEK11】 【DAY4】Employee Management System Part 5【English Version】

2024.5.9 Thursday
Continued from 【WEEK11】 【DAY3】Employee Management System Part 4【English Version】

10.6. Add Employee

10.6.1. Modify list.html

On line 53, change <h2>Section title</h2> to add an employee button.
Insert image description here

<h2><a class="btn btn-sm btn-success" th:href="@{/emp}">Add Employee</a></h2>

10.6.2. Modify EmployeeController.java

Add a new method.

package com.P14.controller;

import com.P14.dao.DepartmentDao;
import com.P14.dao.EmployeeDao;
import com.P14.pojo.Department;
import com.P14.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 java.util.Collection;

@Controller
public class EmployeeController {
    // Query all employees
    @Autowired
    EmployeeDao employeeDao;
    @Autowired  // Autowire
    DepartmentDao departmentDao;

    @RequestMapping("/emps")    // Whenever dashboard.html requests th:href="@{emps}" (line 89), it will be redirected to @RequestMapping("/emps")
    public String list(Model model){    // Then it will query all employees, and modify: how to display them on the front-end page
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    @GetMapping("/emp") // Get request for redirection
    public String toAddpage(Model model){
        // Retrieve information of all departments
        Collection<Department> departments = departmentDao.getDepartment();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        // Adding operation forward
        System.out.println("save=>"+employee);
        employeeDao.save(employee); // Call the underlying business method to save employee information
        return "redirect:/emps";    // After clicking "Add" on the "Add Employee" page, redirect to the "Employee Management" page
    }
}

10.6.3. Create add.html

Insert image description here
Compared with list.html, only the content within <main> needs to be modified.

<body>
   <div th:replace="~{common/commons::topbar}"></div>
   <!-- Change to insert the topbar part from commons.html -->
   <div class="container-fluid">
      <div class="row">
         <div th:replace="~{common/commons::sidebar(active='list.html')}"></div>
         <!-- Change to insert the sidebar part from commons.html -->
         <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>LastName</label>
                  <input type="text" name="lastName" class="form-control" placeholder="SpongeBob">
               </div>
               <div class="form-group">
                  <label>Email</label>
                  <input type="email" name="email" class="form-control" placeholder="987654321@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">Female</label>
                  </div>
               </div>
               <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="gender" value="0">
                  <label class="form-check-label">Male</label>
               </div>
               <div class="form-group">
                  <label>Department</label>
                  <select class="form-control" name="department.id">
                     <!-- We are receiving an Employee in the controller, so we need to submit one of its properties (department.id) -->
                     <option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}"></option>
                  </select>
               </div>
               <div class="form-group">
                  <label>Birth</label>
                  <input type="text" name="birth" class="form-control" placeholder="2020/07/25 18:00:00">
               </div>
               <button type="submit" class="btn btn-primary">Add</button>
            </form>
         </main>
      </div>
   </div>

   ...

</body>

10.6.4. Restart and Run

After logging in, select “Employee Management” -> “Add Employee” -> Fill in the relevant information -> Click “Add”
Insert image description here
Default page:
Insert image description here

After entering information, click “Add”:
Insert image description here
Note that the format for the addition date is yyyy/MM/dd, not yyyy-MM-dd.
Insert image description here

Console log:
Insert image description here

  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值