SpringMVC form 标签

使用 form 标签可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显
 注意:
可以通过 modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
如果该属性值也不存在,则会发生错误。

实例

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<form:form action="${pageContext.request.contextPath }/emp" method="POST"
modelAttribute="employee">

<form:errors path="*"></form:errors>
<br>

<c:if test="${employee.id == null }">
<!-- path 属性对应 html 表单标签的 name 属性值 -->
LastName: <form:input path="lastName"/>
<form:errors path="lastName"></form:errors>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT"/>
<%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>
<%--
<form:hidden path="_method" value="PUT"/>
--%>
</c:if>

<br>
Email: <form:input path="email"/>
<form:errors path="email"></form:errors>
<br>
<%
Map<String, String> genders = new HashMap();
genders.put("1", "Male");
genders.put("0", "Female");

request.setAttribute("genders", genders);
%>
Gender:
<br>
<form:radiobuttons path="gender" items="${genders }" delimiter="<br>"/>
<br>
Department: <form:select path="department.id"
items="${departments }" itemLabel="departmentName" itemValue="id"></form:select>
<br>

Birth: <form:input path="birth"/>
<form:errors path="birth"></form:errors>
<br>
Salary: <form:input path="salary"/>
<br>
<input type="submit" value="Submit"/>
</form:form>

原文地址:https://www.cnblogs.com/huy360/p/4521330.html