//修改用户信息显示页面
@GetMapping("/emp/{id}")
public String modifyView(@PathVariable Integer id, Model model){
model.addAttribute("emp", dao.get(id));
Collection<Department> collection = departmentDao.getDepartments();
model.addAttribute("deparms",collection);
return "employee/add";
}
//修改用户信息
@PutMapping("/emp")
public String modifyEmployee(Employee employee) {
System.out.println("修改用户Controller:"+employee);
Employee temp = dao.get(employee.getId());
if(null!=temp){
temp.setLastName(employee.getLastName());
temp.setEmail(employee.getEmail());
temp.setGender(employee.getGender());
temp.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
temp.setBirth(employee.getBirth());
}
return "redirect:/emps";
}
修改添加二合一表单
<form th:action="@{/emp}" method="post">
<input th:type="hidden" th:if="${emp!=null}" name="id" th:value="${emp.id}">
<input th:type="hidden" th:if="${emp!=null}" name="_method" value="put">
<div class="form-group">
<label>LastName</label>
<input type="text" class="form-control" name="lastName" value="zhangsan" th:value="${emp!=null}?${emp.lastName}">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" value="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:selected="${emp!=null}?${dep.id == emp.department.id}" th:each="dep:${deparms}" th:value="${dep.id}" th:text="${dep.departmentName}">1</option>
</select>
</div>
<div class="form-group">
<label>Birth</label>
<input type="text" class="form-control" name="birth" value="2019-7-5" th:value="${emp!=null}?${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}">
</div>
<button type="submit" class="btn btn-primary" th:text="${emp!=null} ?'修改':'添加'" ></button>
</form>
本文详细介绍了如何使用Spring MVC实现用户信息的修改和添加功能,包括控制器方法的定义、页面表单的设计,以及如何通过Thymeleaf模板引擎进行条件渲染。
241

被折叠的 条评论
为什么被折叠?



