spring的form标签只能依赖于jsp使用,这里只是新见到的,稍微做下记录,也不纠结原理,就记录下用法
后端通过Model存数据
import org.springframework.ui.Model;
//这里是点击修改后,查询到数据,去往修改页面,数据都绑定在model中
@RequestMapping("/admin/to/edit/page")
public String toEditPage(@RequestParam("adminId")Integer adminId,Model model) {
Admin admin = adminService.getAdminById(adminId);
model.addAttribute("admin",admin);
return "admin-edit";
}
通过form标签自动取数据填充
jsp页面导入form标签
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
form:form表单
form:form标签其实就是一个表单,只不过是spring的
modelAttribute:后端存入model的名称,这里存入的是一个对象
< form:input >:这里就是form的input标签
path:存入mode的对象的属性
cssClass:相当于class属性
最主要的是后端在model存入对象,前端form:form中的modelAttribute属性指定后端存入model对象的名称,form:input中的path指定这个对象的属性,可以拿到对应属性的值,cssClass指定class,直接跳转过来以后,属性就会自动填充
<form:form action="admin/update.html" method="post" modelAttribute="admin">
<form:hidden path="id"/>
<div class="form-group">
<label for="exampleInputPassword1">登陆账号</label>
<form:input path="loginAcct" cssClass="form-control" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">登录密码</label>
<form:input path="userPswd" cssClass="form-control" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">用户昵称</label>
<form:input path="userName" cssClass="form-control" />
</div>
<div class="form-group">
<label for="exampleInputEmail1">邮箱地址</label>
<form:input cssClass="form-control" path="email" />
</div>
<button type="submit" class="btn btn-success">
<i class="glyphicon glyphicon-edit"></i> 更新
</button>
<button type="reset" class="btn btn-danger">
<i class="glyphicon glyphicon-refresh"></i> 重置
</button>
</form:form>
没有任何操作,跳转过来,它就应经自动填充好了