SpringMVC - 表单标签form

   1. jsp中引入表单标签

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

   2. jsp中写<form:input>标签

 <form:form cssClass="form-horizontal" action="${pageContext.request.contextPath}/roles/${role.id}" method="post" modelAttribute="role">
	   <input type="hidden" name="_method" value="${empty role.id ? 'post' : 'put'}"/>
	   <form:hidden path="id"/>
	   <form:input path="name" cssClass="form-control"  placeholder="名称"/>
</form:form>

modelAttribute:从Model对象中获取对应属性名的属性值

path:从上述的属性值对象中获取属性

所以一旦你写了modelAttribute="" 对应的属性名的属性值,则必须要提供对象,否则会抛出你个搞不懂的鬼东西,如下面这个玩意


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ukbAHpgk-1587881678317)(en-resource://database/16550:1)]

搞不懂它为什么不报空指针,难顶,查bug查了半天。


  3. jsp中写<form:select>

<form:form modelAttribute="user">
    <form:select cssClass="form-control" path="roles" items="${roleList}" itemLabel="name" itemValue="id"></form:select>
</form:form>

path:roles → 等价于:Model.getAttribute("user).getRoles();
items=${roleList} → 等价于:四大作用域.getAttribute(“roleList”)
itemLabel=“name”: 被<option>包裹的标签值 – 等价于 items=${roleList}遍历的对象.getName()
itemValue=“id”: <option>标签的属性value的值 – 等价于 items=${roleList}遍历的对象.getId()

猜你喜欢

转载自blog.csdn.net/weixin_39651356/article/details/105768097