在jsp页面需要引入:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>标签.
文本框:
<td><form:label path="name">姓名:</form:label></td>
<td><form:input path="name" /></td>
密码框:
<td><form:label path="username">用户名:</form:label></td>
<td><form:input path="username" /></td>
文本域:
<td><form:label path="address">地址:</form:label></td>
<td><form:textarea path="address" rows="5" cols="30" /></td>
复选框:
<td><form:label path="receivePaper">订阅新闻?</form:label></td>
<td><form:checkbox path="receivePaper" /></td>
复选框(多选)需要跟后端java连用:
@ModelAttribute("webFrameworkList")
public List<String> getWebFrameworkList()
{
List<String> webFrameworkList = new ArrayList<String>();
webFrameworkList.add("Spring MVC");
webFrameworkList.add("Spring Boot");
webFrameworkList.add("Struts 2");
webFrameworkList.add("Apache Hadoop");
return webFrameworkList;
}
<form:checkboxes items="${webFrameworkList}" path="favoriteFrameworks" />
单选按钮:
<form:radiobutton path="gender" value="M" label="男" />
<form:radiobutton path="gender" value="F" label="女" />
多项单选按钮(需要跟后端java连用):
@ModelAttribute("numbersList")
public List<String> getNumbersList()
{
List<String> numbersList = new ArrayList<String>();
numbersList.add("1");
numbersList.add("2");
numbersList.add("3");
numbersList.add("4");
return numbersList;
}
<form:radiobuttons path="favoriteNumber" items="${numbersList}" />
下拉列表:需要跟后端代码连用:
@ModelAttribute("countryList")
public Map<String, String> getCountryList()
{
Map<String, String> countryList = new HashMap<String, String>();
countryList.put("US", "United States");
countryList.put("CH", "China");
countryList.put("SG", "Singapore");
countryList.put("MY", "Malaysia");
return countryList;
}
<form:select path="country">
<form:option value="NONE" label="Select"/>
<form:options items="${countryList}" />
</form:select>
多列表复选框:
@ModelAttribute("skillsList")
public Map<String, String> getSkillsList()
{
Map<String, String> skillList = new HashMap<String, String>();
skillList.put("Hibernate", "Hibernate");
skillList.put("Spring", "Spring");
skillList.put("Apache Hadoop", "Apache Hadoop");
skillList.put("Struts", "Struts");
return skillList;
}
<form:select path="skills" items="${skillsList}" multiple="true" />
隐藏域:
<form:hidden path="id" value="1000"/>
spring mvc错误处理 :
<form:errors path="*" cssClass="errorblock" element="div" />
上传文件:
<form:form method="POST" modelAttribute="fileUpload"
enctype="multipart/form-data">
请选择一个文件上传 :
<input type="file" name="file" />
<input type="submit" value="提交上传" />
</form:form>
这里使用带有value =“fileUpload”
的modelAttribute
属性来映射文件用服务器模型上传控件。