Spring MVC 学习笔记 第八章 Spring MVC表单标签库
一.表单标签库引入
- Handler
package com.fw.controller;
import com.fw.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/tag")
public class TagHandler {
@RequestMapping("/get")
public ModelAndView get(){
ModelAndView modelAndView = new ModelAndView("show");
Student student =new Student();
student.setId(100);
student.setName("花花");
student.setAge(20);
modelAndView.addObject("student",student);
return modelAndView;
}
@RequestMapping("/get2")
public ModelAndView get2(){
ModelAndView modelAndView = new ModelAndView("tag");
Student student =new Student();
student.setId(100);
student.setName("花花");
student.setAge(20);
modelAndView.addObject("student",student);
return modelAndView;
}
}
- JSP
①未使用表单标签
<%--
Created by IntelliJ IDEA.
User: fw
Date: 2020/5/1
Time: 16:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<form>
学生ID :<input type="text" name="id" value="${student.id}"><br>
学生姓名:<input type="text" name="name" value="${student.name}"><br>
学生年龄:<input type="text" name="age" value="${student.age}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
②引入表单标签库
<%--
Created by IntelliJ IDEA.
User: fw
Date: 2020/5/1
Time: 16:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<f:form modelAttribute="student">
学生ID :<f:input path="id"/><br>
学生姓名:<f:input path="name"/><br>
学生年龄:<f:input path="age"/><br>
<input type="submit" value="提交">
</f:form>
</body>
</html>
- JSP页面导入Spring MVC 标签库,与导入JSTL标签库的语法相似,前缀prefix可以自定义,通常定义为form(本文中的例子定义成了f).
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
- 将form表单与模型数据进行绑定,通过modelAttribute属性完成绑定 ,将modelAttribute的值设置为模型数据对于的key值.
<f:form modelAttribute="student">
- form表单完成绑定之后,将模型数据的值取出绑定到不同的标签中,通过设置标签的path属性完成,将path属性的值设置为模型数据对应的属性名即可。
学生ID :<f:input path="id"/><br>
二. 常用的表单标签
- from
<f:form modelAttribute="student">
渲染的是HTML中的,通过modelAttribute属性来绑定具体的模型数据。
- input
<f:input path="id"/>
渲染的是HTML中的,form标签绑定的是模型数据,input标签绑定的是模型数据中的属性值,通过path属性可以与模型数据中的属性名对应,并支持级联操作。
<f:input path="address.name"/>
- password
<f:password path="password"/>
渲染的是HTML中的,通过path属性可以与模型数据中的属性名对应,password标签的值不会在页面显示。
- checkbox
<f:checkbox path="hobby" value=“读书”/>
渲染的是HTML中的,通过path属性可以与模型数据中的属性名对应,可以绑定boolean,数组和集合。
如果绑定 boolean值,若该变量的值为true,则该复选框选择,否则不选中。
如果绑定数组或集合,数组或集合中的元素等于checkbox的value值,则选中。
entity实体类(Student.java):
package com.fw.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class Student {
private long id;
private String name;
private int age;
private String[] hobby;
}
Handler(TagHandler.java):
@RequestMapping("/get2")
public ModelAndView get2(){
ModelAndView modelAndView = new ModelAndView("tag");
Student student =new Student();
student.setId(100);
student.setName("花花");
student.setAge(20);
String[] hobby = {"读书","画画","唱歌","跳舞"};
student.setHobby(hobby);
modelAndView.addObject("student",student);
return modelAndView;
}
或者将String数组改为List也是一样的效果。
student.setHobby(Arrays.asList("读书","画画","唱歌","跳舞"));
JSP(tag.jsp):
<%--
Created by IntelliJ IDEA.
User: fw
Date: 2020/5/1
Time: 16:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<f:form modelAttribute="student">
学生ID :<f:input path="id"/><br>
学生姓名:<f:input path="name"/><br>
学生年龄:<f:input path="age"/><br>
爱好:<f:checkbox path="hobby" value="读书"></f:checkbox>读书
<f:checkbox path="hobby" value="画画"></f:checkbox>画画
<f:checkbox path="hobby" value="唱歌"></f:checkbox>唱歌
<f:checkbox path="hobby" value="听音乐"></f:checkbox>听音乐
<f:checkbox path="hobby" value="跳舞"></f:checkbox>跳舞
<f:checkbox path="hobby" value="旅游"></f:checkbox>旅游<br>
<input type="submit" value="提交">
</f:form>
</body>
</html>
运行结果:
☼注:
<f:checkbox path=“hobby” value=“读书”></f:checkbox>读书
的最后面的读书是用来显示到页面上,注意是写在checkbox标签外的,一定不要写到开始结束标签之间。
- checkboxes
<f: checkboxes items=${student.hobby} path=”selectHobby” />
渲染的是HTML中的一组,是对<f:checkbox>的一种简化,需要结合items和path属性来使用。items绑定被遍历的结合或数组,path绑定被选中集合或数组,可以理解items为全部的可选数组集合,path为默认的选中集合。
Handler(TagHandler.java):
package com.fw.entity;
import lombok.Data;
import java.util.List;
@Data
public class Student {
private long id;
private String name;
private int age;
private List<String> hobby;
private List<String> selectHobby;
}
@RequestMapping("/get2")
public ModelAndView get2(){
ModelAndView modelAndView = new ModelAndView("tag");
Student student =new Student();
student.setId(100);
student.setName("花花");
student.setAge(20);
student.setHobby(Arrays.asList("读书","画画","唱歌","听音乐","跳舞","旅游"));
student.setSelectHobby(Arrays.asList("唱歌","跳舞","旅游"));
modelAndView.addObject("student",student);
return modelAndView;
}
JSP(tag.jsp):
<%--
Created by IntelliJ IDEA.
User: fw
Date: 2020/5/1
Time: 16:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<f:form modelAttribute="student">
学生ID :<f:input path="id"/><br>
学生姓名:<f:input path="name"/><br>
学生年龄:<f:input path="age"/><br>
爱好:<f:checkboxes path="selectHobby" items="${student.hobby}"></f:checkboxes><br>
<input type="submit" value="提交">
</f:form>
</body>
</html>
运行结果:
需要注意的是path可以直接绑定模型数据的属性值,items则需要通过EL的形式从域对象中获取数组,不能直接写属性名。
- radiobutton:
<f:radiobutton path="sex" value=0 />
渲染的是HTML中的一个,绑定的数据域标签的value值相等则为选中状态,否则不选中。
entity实体类(Student.java)添加属性sex:
private int sex;
Handler(TagHandler.java):
student.setSex(1);
JSP(tag.jsp):
学生性别:<f:radiobutton path="sex" value="0"></f:radiobutton>男
<f:radiobutton path="sex" value="1"></f:radiobutton>女<br>
运行结果:
- radiobuttons
<f:radiobuttons path="selectGrade" items="${student.gradeMap}">
渲染的是HTML中的一组,需要结合items和path属性来使用。items绑定被遍历的结合或数组,path绑定被选中的值, items为全部的可选类型,path为默认选中的选项。用法与<form: checkboxes/>一致。
entity实体类(Student.java):
private Map<Integer,String> gradeMap;
private int selectGrade;
Handler(TagHandler.java):
Map<Integer,String> gradeMap = new HashMap<Integer,String>();
gradeMap.put(1,"一年级");
gradeMap.put(2,"二年级");
gradeMap.put(3,"三年级");
gradeMap.put(4,"四年级");
gradeMap.put(5,"五年级");
gradeMap.put(6,"六年级");
student.setGradeMap(gradeMap);
student.setSelectGrade(3);
modelAndView.addObject("student",student);
JSP(tag.jsp):
<body>
<h1>学生信息</h1>
<f:form modelAttribute="student">
学生ID :<f:input path="id"/><br>
学生姓名:<f:input path="name"/><br>
学生年龄:<f:input path="age"/><br>
学生性别:<f:radiobutton path="sex" value="0"></f:radiobutton>男
<f:radiobutton path="sex" value="1"></f:radiobutton>女<br>
学生年级:<f:radiobuttons path="selectGrade" items="${student.gradeMap}"></f:radiobuttons><br>
爱好:<f:checkboxes path="selectHobby" items="${student.hobby}"></f:checkboxes><br>
<input type="submit" value="提交">
</f:form>
</body>
运行结果:
- select
<f:select path="selectCity" items="${student.cityMap}">
渲染的是HTML中的一组,需要结合items和path属性来使用。items绑定被遍历的结合或数组,path绑定被选中的值, 用法与<f:radiobuttons>是一样的
entity实体类(Student.java):
private Map<Integer,String> cityMap;
private int selectCity;
Handler(TagHandler.java):
Map<Integer,String> cityMap = new HashMap<Integer,String>();
cityMap.put(1,"北京");
cityMap.put(2,"上海");
cityMap.put(3,"深圳");
cityMap.put(4,"杭州");
cityMap.put(5,"重庆");
student.setCityMap(cityMap);
student.setSelectCity(4);
modelAndView.addObject("student",student);
JSP(tag.jsp):
<f:form modelAttribute="student">
学生ID :<f:input path="id"/><br>
学生姓名:<f:input path="name"/><br>
学生年龄:<f:input path="age"/><br>
学生性别:<f:radiobutton path="sex" value="0"></f:radiobutton>男
<f:radiobutton path="sex" value="1"></f:radiobutton>女<br>
学生年级:<f:radiobuttons path="selectGrade" items="${student.gradeMap}"></f:radiobuttons><br>
爱好:<f:checkboxes path="selectHobby" items="${student.hobby}"></f:checkboxes><br>
居住城市:<f:select path="selectCity" items="${student.cityMap}"></f:select>
<input type="submit" value="提交">
</f:form>
运行结果:
- options:
<f:select>结合<f:options>的使用,<f:select>只定义path属性,在<f:select>标签内部添加一个子标签<f:options>,设置items属性,获取被遍历的集合。
JSP(tag.jsp):
居住城市:<f:select path="selectCity">
<f:options items="${student.cityMap}"></f:options>
</f:select>
- option
<f:select>结合<f:option>的使用,<f:select>定义path属性,给每一个<f:option>设置value值,path的值与那个value值相等,该项默认选中。
居住城市:<f:select path="selectCity">
<f:option value="1">广州</f:option>
<f:option value="2">南京</f:option>
<f:option value="3">大连</f:option>
</f:select>
- textarea:
渲染的是HTML< textarea/>,path绑定模型数据的属性值,作为文本输入域的默认值。
entity实体类(Student.java):
private String introduce;
Handler(TagHandler.java):
student.setIntroduce("你好呀");
JSP(tag.jsp):
信息:<f:textarea path="introduce"/><br>
- errors
处理错误信息,一般用在数据校验,该标签需要结合Spring MVC的验证器一起使用。