SpringMVC 的表单标签库
程序员文章站
2022-04-29 17:19:30
...
1.form标签
2.input标签
例子:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form:form modelAttribute="user" action="register" method="post">
<table>
<tr>
<td>名字:</td>
<td><form:input path="name"/></td>
</tr>
<tr>
<td>性別:</td>
<td><form:input path="sex"/></td>
</tr>
<tr>
<td>年齡:</td>
<td><form:input path="age"/></td>
</tr>
</table>
</form:form>
然后在controller包中的方法中加入:
model.addAttribute("user",user);
3.password标签
<form:password path="password"/>
<!-- 上述的springmvc的标签会被设置成下方的html格式-->
<input name="password" type="password"/>
4.hidden标签
<form:hidden path="id"/>
<!-- 上述的springmvc的标签会被设置成下方的html格式-->
<input name="id" type="hidden"/>
5.textarea标签
<form:textarea path="book" cols="12" rows="12"/>
<!-- 上述的springmvc的标签会被设置成html中的-->
<textarea rows="12" cols="12" name="book"></textarea>
6.CheckBox标签
列表数据可以包括List,Set和数组。value值在我们绑定中的数据时,CheckBox的状态为选中。
<form:checkbox path="coures" label="java" value="java"/>
<form:checkbox path="coures" label="Golang" value="Golang"/>
<form:checkbox path="coures" label="CPP" value="CPP"/>
7.checkboxes标签
8.radiobutton标签
9.radiobuttons标签
一个小栗子:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
放在src目录下的springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.majun"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
放在src目录下的不同包下两个实体类:package com.majun.controller;//控制层的包
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.majun.entity.*;
@Controller
@RequestMapping("spring")
public class Test {
@RequestMapping(value="/testRadiobuttons",method=RequestMethod.GET)
public String testRadiobuttons(Model model) {
User user=new User();
user.setSex("男");
List<String> sexList=new ArrayList<String>();
sexList.add("男");
sexList.add("女");
model.addAttribute("user",user);
model.addAttribute("sexList",sexList);
return "testRadiobuttons";
}
}
package com.majun.entity;//实体类
public class User {
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
放在WEB-INF目录下的views文件夹下(视图层):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>hello</h2>
<form:form modelAttribute="user" method="post" action="testRadiobuttons">
<table>
<tr>
<td>性别:</td>
<td><form:radiobuttons path="sex" items="${sexList }"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
10.select标签
11.option标签
12.options标签
13.erros标签
上一篇: form表单的底部居中
推荐阅读
-
解决表单post,get到springMVC后台乱码的问题
-
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
-
目前为止用户体验度最好的表单:浮动标签式的表单
-
Html5新标签datalist实现输入框与后台数据库数据的动态匹配
-
解决IE9在线打开文档库中的InfoPath表单出错问题
-
SpringMVC 向jsp页面传递数据库读取到的值方法
-
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
-
JSP中操作数据库的常用SQL标签用法总结
-
jsp通过自定义标签库实现数据列表显示的方法
-
Jsp开发基础知识+九大隐式对象+11个内置对象+标签库(实际web开发中的典型)