thymeleaf对于HTML页面中关于文本框、下拉框、单选按钮赋值处理

该篇博客展示了如何在EmployeeController中使用RESTful风格的方法`toUpdate`来更新员工信息。通过`@PathVariable`接收ID,从DAO层获取Department和Employee对象,设置部门关系并传递到前端`update.html`页面。在前端页面,使用Thymeleaf模板引擎展示表单,包括文本框、单选按钮、下拉框和日期输入,允许用户编辑并提交员工的详细信息。

记个笔记

EmployeeConotroller.java

    /**
     * 使用RestFul风格
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        Department departments = departmentDao.getDepartmentsById(id);
        System.out.println(employeeDao.getEmployeeById(id)+"======>");
        Employee employee = employeeDao.getEmployeeById(id);
        employee.setDepartment(departments);
        model.addAttribute("emp",employee);
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("department",department);
        return "emp/update";
    }

update.html

	<body>
	<!--这里的侧边栏和头部都是获取的公共组件-->
		 <div th:replace="~{commons/commons::navbar}"></div>
		<div class="container-fluid">
			<div class="row"> 
				<!--侧边栏-->
				<div th:replace="~{commons/commons::sidebar}"></div>
				<!--主内容-->
				<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
					<h2>员工添加</h2>
					<form method="post" action="/add">
						<div class="form-group">
							<label>用户名:</label>
							<!--文本框的赋值-->
							<input type="text" class="form-control" placeholder="lastName" name="lastName" th:value="${emp.getLastName()}">
						</div>
						<div class="form-group">
							<label for="exampleInputEmail1">邮箱地址</label>
							<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="email" th:value="${emp.getEmail()}">
						</div>
						<!--单选按钮的赋值-->
						<div class="form-group" th:value="${emp.getGender()}">
							<label class="radio-inline">
								<input type="radio" name="gender" id="inlineRadio1" th:checked="${emp.getGender()==1}"></label>
							<label class="radio-inline">
								<input type="radio" name="gender" id="inlineRadio2"  th:checked="${emp.getGender()==0}"></label>
						</div>
						<!--下拉框的取值赋值-->
						<div class="form-group">
							<select class="form-control" name="department.id">
								<option th:each="dept:${department}" th:value="${dept.getId()}"
										th:selected="${dept.getId()==emp.getDepartment().getId()}">[[${dept.getDepartmentName()}]]</option>
							</select>
						</div>
						<!--对于日期格式的处理-->
						<div class="form-group">
							<label>生日:</label>
							<input type="text" class="form-control" placeholder="birth" name="birth" th:value="${#dates.format(emp.getBirth(),'yyyy-MM-dd')}">
						</div>
						<button type="submit" class="btn btn-default">Submit</button>
					</form>
				</main>
			</div>
		</div>
 
	</body>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值