我們說到新增功能
他應該要包含兩個步驟
1 點進去進入到一個可以填寫空白資訊的頁面(employee>Get)
2 輸入完新增的資訊然後按下儲存鍵, 完成新增項目的儲存(employee>Post)
從畫面過來, 一開始是點擊這個標籤
<a href="emp" th:href="@{/emp}" class="btn btn-sm btn-success">員工添加</a>
然後會進到controller, 因為裡面會要將所有可以選取的Department顯示出來,
所以要先getDepartments再放到model中
@GetMapping("/emp")
public String toAddPage(Model model){
// 來到添加頁面, 查出所有的部門, 在頁面顯示
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("depts",departments);
return "emp/add";
}
這裡的Department類別
public class Department {
private Integer id;
private String departmentName;
}
這是Dao
@Repository
public class DepartmentDao {
private static Map<Integer, Department> departments = null;
static{
departments = new HashMap<Integer, Department>();
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();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
然後畫面上的form是這樣
<form th:action="@{/emp}" method="post">
<input type="hidden" name="_method" value="put" th:if="${emp != null}">
<input type="hidden" name="id" th:value="${emp != null} ? ${emp.id}">
<div class="form-group">
<label>LastName</label>
<input name="lastName" type="text" class="form-control" placeholder="zhangsan" th:value="${emp != null} ? ${emp.lastName}">
</div>
<div class="form-group">
<label>Email</label>
<input name="email" type="email" class="form-control" placeholder="zhangsan@atguigu.com" th:value="${emp != null} ? ${emp.email}">
</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" th:checked="${emp != null} ? ${emp.gender == 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" th:checked="${emp != null} ? ${emp.gender == 0}">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label>department</label>
<select class="form-control" name="department.id">
<option th:value="${dept.id}" th:each="dept : ${depts}" th:text="${dept.departmentName}" th:selected="${emp != null} ? ${dept.id == emp.department.id}"></option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input name="birth" type="text" class="form-control" placeholder="zhangsan" th:value="${emp != null} ? ${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm:ss')}">
</div>
<button type="submit" class="btn btn-primary" th:text="${emp != null} ? '修改' : '添加'"></button>
</form>
其中我們可以看到拿出depts塞到dept中, 他會需要將id塞到value中, 要特別注意一下
那麼他送出, 就是一個Employee的model
這樣畫面上是使用name來做對應屬性
而department則要用department.id來對應
這個form發出的是Post請求
@PostMapping("/emp")
public String addEmp(Employee employee){
System.out.println("保存的員工信息: "+employee);
employeeDao.save(employee);
return "redirect:/emps";
}
但是記得進入的時候要重新導向redirect
而以下是dao的 save功能
private static Integer initId = 1006;
public void save(Employee employee){
if(employee.getId() == null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
還有一個日期要做補充
通常預設日期會為20xx/xx/xx
如果要格式化日期, 是可以在application.properties中做控制的, 如以下
spring.mvc.date-format=yyyy-mm-dd
沒有留言:
張貼留言