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>サーブレット-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)
{
      //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";
}
}

4、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>

5XMLファイルで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">
    <!-- コンポーネントスキャニングのサポートを提供 -->
    <context:component-scan base-package="com.w3codebox" />
    <!--変換、フォーマット、バリデーションのサポートを提供 -->
    <mvc:annotation-driven/>
    <!-- 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、リクエストのページの作成

index.jsp

<!DOCTYPE html>
<html>
<head>
    <title>鉄道登録フォーム</title>
</head>
<body>
<a href="reservation/bookingForm">予約はこちらをクリックしてください.</a>
</body>
</html>

7、他のビューコンポーネントの作成

reservation-page.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/タグ/form" %>
<!DOCTYPE html>
<html>
<head>
    <title>予約フォーム</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-詳細を確認してください.</p>
First Name : ${reservation.firstName} <br>
Last Name : ${reservation.lastName}
</body>
</html>

出力: