springboot+thymeleaf:实现更新信息

这里实现更新信息的思路是:从所有员工信息页面(info.html),点击员工信息后的修改按钮,跳转到修改信息页面(update.html),填写修改信息提交到controller中的updateById方法,修改完成跳转回主页面(main.html)
在这里插入图片描述
在这里插入图片描述
这里有一个问题,按照id更新,最后执行updateById方法时,需要知道id才行,那么就需要,info跳转到update时将id作为参数传递过去,这倒是不难<a th:href="'update.html?id='+${user.getId()}">这样就可以将id传过去,但在update中取值却有点麻烦,查询资料都是需要使用js等帮助完成,但我只想简单的取个值(有知道简单操作的同学欢迎指出)。既然update页面无法获取到id值,更不用说提交后将id值传递给updateById方法了。后来只好转变思路,info不直接跳转到update,先跳转到一个controller(因为HTML和controller之间传值还是比较容易的),将id值传给这个controller,这个controller再跳转到update页面,同时将id值传过去,这样update页面就能获取到id值,也就能传给updateById方法了。
controller类

@RequestMapping("updateById")
	public String updateById(Employee employee,HttpServletRequest request) {
		Integer id = Integer.parseInt(request.getParameter("id"));
		employee.setId(id);
		employeeMapper.updateById(employee);
		return "main";
	}
	
	@RequestMapping("sendId")
	public String sendId(HttpServletRequest request,Model model) {
		Integer id = Integer.parseInt(request.getParameter("id"));
		model.addAttribute("id", id);
		return "update";
	}

info.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="shortcut icon" th:href="@{/static/favicon.ico}" />

<meta charset="UTF-8">
<title>所有员工信息页面</title>
</head>
<body>
	<table>
		<tr>
			<th>ID</th>
			<th>名字</th>
			<th>邮箱</th>
			<th>性别</th>
			<th>年龄</th>
		</tr>
		<tr th:each="user:${users}">
			<!-- 
			<td th:text="${user.id}"></td>
			<td th:text="${user.lastName}"></td>
			<td th:text="${user.email}"></td>
			<td th:text="${user.gender}"></td>
			<td th:text="${user.age}"></td>
		-->
			<td th:text="${user.getId()}"></td>
			<td th:text="${user.getlastName()}"></td>
			<td th:text="${user.getEmail()}"></td>
			<td th:text="${user.getGender()}"></td>
			<td th:text="${user.getAge()}"></td>
			<!--<td><a th:href="'update.html?id='+${user.getId()}">更新信息</a></td>-->
			<td><a th:href="@{sendId(id=${user.getId()})}">更新信息</a></td>
			<td><a href="#" th:href="@{deleteById(id=${user.getId()})}">删除员工</a></td>
		</tr>
	</table>
</body>
</html>

update.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="shortcut icon" th:href="@{/static/favicon.ico}" />

<meta charset="UTF-8">
<title>更新信息</title>
</head>

<body>
		<form th:action="@{updateById(id=${id})}" method="post">
		<input type="hidden" name="id" value="${id}">
		姓名:<input type="text" name="lastName"/><br>
		年龄:<input type="text" name="age"/><br>
		邮箱:<input type="text" name="email"/><br>
		<input type="submit" value="提交"/><br>
		</form>
</body>
</html>

如果有更好的写法欢迎指出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值