English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

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表单文本字段的示例

让我们看一个使用表单标签库创建铁路预订表单的示例。

1、将依赖项添加到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类包含与表单中存在的输入字段相对应的变量(以及setter和getter方法)。

Reservation.java

package com.w;3codebox;
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;
    }   
}

3、创建控制器类

ReservationController.java

package com.w;3codebox;
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)
{
      //créer un objet réservation 
    Reservation res = new Reservation();
      //fournir l'objet réservation au modèle 
    model.addAttribute("reservation", res);
    return "reservation-page";
}
@RequestMapping("/submitForm())
// @ModelAttribute lie le données du formulaire à l'objet
public String submitForm(@ModelAttribute("reservation") Reservation res)
{
    return "confirmation-form";
}
}

4、Dans le fichier web.xml fournir les entrées du contrôleur

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>

5Définir Bean dans le fichier xml

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">
    <!-- Fournir un soutien pour le balayage des composants -->
    <context:component-scan base-package="com.w3codebox" />
    <!--Fournir un soutien pour la conversion, le formatage et la validation -->
    <mvc:annotation-driven/>
    <!-- Définir le résolveur de vue Spring MVC -->
     <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>

6、créer la page de demande

index.jsp

<!DOCTYPE html>
<html>
<head>
    <title>Formulaire d'enregistrement des chemins de fer</title>/title>
</head>
<body>
<a href="reservation/bookingForm">Cliquez ici pour la réservation.</a>/a>
</body>
</html>

7、créer d'autres composants de vue

reservation-page.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
    <title>Formulaire de réservation</title>/title>
</head>
<h3>Formulaire de réservation de chemin de fer</h3>
<body>
    <form:form action="submitForm" modelAttribute="reservation">
        Prénom : <form:input path="firstName" />      
        <br><br>
        Nom : <form:input path="lastName" />
        <br><br>
        <input type="submit" value="Soumettre" />  
    </form:form>
</body>
</html>

Attention-Les valeurs transmises via l'annotation @ModelAttribute doivent être identiques aux valeurs modelAttribute existantes dans la page de vue.

confirmation-page.jsp

<!DOCTYPE html>
<html>
<body>
<p Votre réservation est confirmée avec succès. Veuillez, re-Vérifiez les détails.</p>
Prénom : ${reservation.firstName} <br>
Nom de famille : ${reservation.lastName}
</body>
</html>

Sortie :