42、尚硅谷_SpringBoot_web开发-【实验】-员工修改-重用页面&修改完成

本文详细介绍了如何使用Spring MVC实现用户信息的修改和添加功能,包括控制器方法的定义、页面表单的设计,以及如何通过Thymeleaf模板引擎进行条件渲染。
  //修改用户信息显示页面
    @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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值