Spring教程-Spring MVC 表单文本字段
Spring MVC 表单文本字段
Spring MVC 表单文本字段标签生成使用绑定值的 HTML 输入标签。默认情况下,输入标签的类型是文本。
语法
<form:input path="name" />
这里,path 属性将表单字段绑定到 Bean 属性。
Spring MVC 表单标签库还提供其他输入类型,如电子邮件、日期、电话等。
对于电子邮件:
<form:input type="email" path="email" />
对于日期:
<form:input type="date" path="date" />
Spring MVC 表单文本字段示例 让我们看一个使用表单标签库创建火车预订表单的示例。
- 在 pom.xml 文件中添加依赖项。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.12</version>
</dependency>
2.创建 Bean 类 这里,Bean 类包含与表单中的输入字段对应的变量(以及设置器和获取器方法)。
Reservation.java
package cn.javatiku;
public class Reservation {
private String firstName;
private String lastName;
public Reservation()
{
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
- 创建控制器类 ReservationController.java
package cn.javatiku;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/reservation")
@Controller
public class ReservationController {
@RequestMapping("/bookingForm")
public String bookingForm(Model model)
{
//create a reservation object
Reservation res=new Reservation();
//provide reservation object to the model
model.addAttribute("reservation", res);
return "reservation-page";
}
@RequestMapping("/submitForm")
// @ModelAttribute binds form data to the object
public String submitForm(@ModelAttribute("reservation") Reservation res)
{
return "confirmation-form";
}
}
- 在 web.xml 文件中提供控制器的入口
web.xml
<?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>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 在 xml 文件中定义 Bean
spring-servlet.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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Provide support for component scanning -->
<context:component-scan base-package="cn.javatiku" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 创建所需的页面
index.jsp
<!DOCTYPE html>
<html>
<head>
<title>Railway Registration Form</title>
</head>
<body>
<a href="reservation/bookingForm">Click here for reservation.</a>
</body>
</html>
- 创建其他视图组件
reservation-page.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>Reservation Form</title>
</head>
<h3>Railway Reservation Form</h3>
<body>
<form:form action="submitForm" modelAttribute="reservation">
First name: <form:input path="firstName" />
<br><br>
Last name: <form:input path="lastName" />
<br><br>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>
注意 - 通过 @ModelAttribute 注解传递的值应与视图页面中的 modelAttribute 值相同。
confirmation-page.jsp
<!DOCTYPE html>
<html>
<body>
<p>Your reservation is confirmed successfully. Please, re-check the details.</p>
First Name : ${reservation.firstName} <br>
Last Name : ${reservation.lastName}
</body>
</html>
输出:(这里显示了表单和确认页面的内容)
以上就是使用 Spring MVC 表单标签库创建火车预订表单的示例。