1、form标签有一个重要的属性modelAttribute,它的默认值是command,代表此表单绑定的模型属性名称。
<form:form modelAttribute="book" method="post" action="login"></form:form>
此时modelAttribute的值book是一个模型对象
2、input有一个path属性,其值代表要绑定属性的路径
3、password除了path外还有showPassword属性,代表显示或遮盖密码,其默认值是false,即遮盖密码
4、hidden标签生成一个隐藏标签
<form:hidden path="id"/>
5、textarea标签,下面创建一个id为content,共5行30列的文本区域
<form:textarea path="content" rows="5" cols="30"/>
示例:
新建src/pojo/User.java
package kshon.pojo;
public class User{
private String password;
private String username;
private int age;
public User(){}
public User(String password,String username,int age){
this.age = age;
this.password = password;
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
新建WEB-INF/content/form.jsp页面,注意,页面中要加入标签库taglib才能使用
<%@ 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>form标签</title>
</head>
<body>
<form:form method="post" action="register" modelAttribute="user">
姓名: <form:input path="username"/><br>
密码: <form:password path="password" showPassword="true"/><br>
年龄:<form:input path="age"/>
</form:form>
</body>
</html>
新建控制器src/controller/UserController.java
package kshon.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping(value="/register")
public String register(Model model){
System.out.println("register POST被调用");
User user = new User("abc","kshonliao",22);
model.addAttribute("user",user);
return "form";
}
}
在WEB-INF目录下新建springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- spring自动扫描base-package下面的包或子包下面的java文件 -->
<context:component-scan base-package="kshon.controller,kshon.pojo" />
<!-- 设置配置方案 -->
<mvc:annotation-driven />
<!-- 使用默认的servlet响应静态文件 -->
<mvc:default-servlet-handler/>
<!-- 配置annotation类型的处理映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置annotation类型的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/content/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
最后要在web.xml文件中增加监听器ContextLoaderListener,它默认会查找本目录下的applicationContext.xml作为spring的配置文件,但是我们这里使用的名字是springmvc-config.xml,所以我们要加入一个标签context-param配置参数
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- 前端控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation是参数名称,该参数的值包含的是springmvc的配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<!-- 在web应用程序启动时立即加载Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring的核心文件,必须使用context-param,ContextLoaderListener才能找到 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</context-param>
</web-app>
最后运行tomcat,输入http://localhost:8080/SpringMVC/register即可访问
6、checkbox标签(radioButton类似)
属性 | 描述 |
path | 要绑定的路径 |
label | 复选框显示的值 |
value | 绑定的数据列表中存在value的值时,复选框选中 |
修改src/pojo/User.java文件:
package kshon.pojo;
import java.util.List;
public class User{
private boolean reader;
private List<String> book;
public boolean isReader() {
return reader;
}
public void setReader(boolean reader) {
this.reader = reader;
}
public List<String> getBook() {
return book;
}
public void setBook(List<String> book) {
this.book = book;
}
}
修改控制器src/controller/UserController.java
package kshon.controller;
import java.util.ArrayList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping(value="/checkbox")
public String checkbox(Model model){
System.out.println("checkbox POST被调用");
User user = new User();
ArrayList<String> book = new ArrayList<String>();
book.add("Math");
book.add("English");
user.setBook(book);
user.setReader(true);
model.addAttribute("user",user);
return "form";
}
}
修改WEB-INF/content/form.jsp页面
<%@ 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>form标签</title>
</head>
<body>
<form:form method="post" action="register" modelAttribute="user">
所选书籍:<form:checkbox path="book" value="Math" label="数学"/>
<form:checkbox path="book" value="Chinaese" label="语文"/>
<form:checkbox path="book" value="English" label="英语"/><br/>
<form:checkbox path="reader" value="true"/>已经阅读协议
</form:form>
</body>
</html>
启动tomcat,输入http://localhost:8080/SpringMVC/checkbox
7、checkboxes标签(radioButtons)
属性 | 描述 |
path | 要绑定的属性路径 |
items | 生成checkbox元素的对象的集合,这是必须的 |
itemLabel | item属性中定义的Conllection、Map或Array的对象属性,将作为每个checkbox的label |
itemValue | item属性中定义的Conllection、Map或Array的对象属性,将为每个checkbox提供值 |
delimiter | 定义两个Input之间的分隔符,默认没有分隔符 |
修改src/controller/UserController.java,添加一个bookList
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping(value="/checkbox")
public String checkbox(Model model){
System.out.println("checkbox POST被调用");
User user = new User();
ArrayList<String> book = new ArrayList<String>();
book.add("Math");
book.add("English");
user.setBook(book);
user.setReader(true);
ArrayList<String> bookList = new ArrayList<String>();
bookList.add("Chinaese");
bookList.add("Histroy");
bookList.add("Math");
model.addAttribute("bookList",bookList);
model.addAttribute("user",user);
return "form";
}
}
修改WEB-INF/content/form.jsp页面
<%@ 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>form标签</title>
</head>
<body>
<form:form method="post" action="register" modelAttribute="user">
所选书籍:<form:checkboxes items="${bookList }" path="book"/>
</form:form>
</body>
</html>
启动tomcat,输入http://localhost:8080/SpringMVC/checkbox
8、select、option、options标签的使用
修改src/pojo/User.java
package kshon.pojo;
import java.util.List;
public class User{
private int deptId;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
}
新建src/pojo/Dept.java
package kshon.pojo;
public class Dept {
private int id;
private String name;
public Dept(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
修改src/controller/UserController.java
package kshon.controller;
import java.util.ArrayList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import kshon.pojo.Dept;
import kshon.pojo.User;
@Controller
public class UserController {
public UserController(){
super();
}
@RequestMapping("/select")
public String select(Model model){
User user = new User();
user.setDeptId(3); //选择第三个option
ArrayList<Dept> deptList = new ArrayList<Dept>();
deptList.add(new Dept(1,"开发部门"));
deptList.add(new Dept(2,"财务部门"));
deptList.add(new Dept(3,"销售部门"));
deptList.add(new Dept(4,"生产部门"));
model.addAttribute("deptList",deptList);
model.addAttribute("user",user);
return "form";
}
}
修改WEB-INF/content/form.jsp页面
<%@ 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>form标签</title>
</head>
<body>
<form:form method="post" action="register" modelAttribute="user">
选择部门:
<form:select path="deptId">
<form:options items="${deptList}" itemLabel="name" itemValue="id"/>
</form:select>
</form:form>
</body>
</html>
启动tomcat,输入http://localhost:8080/SpringMVC/select