Spring MVC 表单下拉列表

Spring MVC 表单下拉列表包含元素列表。此标签生成 HTML select 元素,允许数据绑定到所选元素。

语法

<form:select path="name">  

以下是用于选择选项的其他标签。

option 标签

此标签生成 HTML option 标签。每个标签都包含用户可以选择的值。

<form:option value="abc" label="xyz"/>  

options 标签

此标签生成一系列 HTML option 标签。每个标签包含用户选择的元素列表。

语法

<form:options items="${elementList}" itemValue="abc" itemLabel="xyz"/>  

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 类

Reservation.java

package cn.javatiku;  
  
public class Reservation {  
  
    private String firstName;  
    private String lastName;  
    private String Gender;  
    private String[] Food;  
    private String cityFrom;  
    private String cityTo;  
    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;  
    }  
    public String getGender() {  
        return Gender;  
    }  
    public void setGender(String gender) {  
        Gender = gender;  
    }  
    public String[] getFood() {  
        return Food;  
    }  
    public void setFood(String[] food) {  
        Food = food;  
    }     
    public String getCityFrom() {  
    return cityFrom;  
}  
public void setCityFrom(String cityFrom) {  
    this.cityFrom = cityFrom;  
}  
public String getCityTo() {  
    return cityTo;  
}  
public void setCityTo(String cityTo) {  
    this.cityTo = cityTo;  
}     
}  

3. 创建控制器类

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")  
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>  

5. 在 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>  

6. 创建所需页面

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>  
        Gender:   
        Male<form:radiobutton path="Gender" value="Male"/>  
        Female<form:radiobutton path="Gender" value="Female"/>  
        <br><br>  
        Meals:  
        BreakFast<form:checkbox path="Food" value="BreakFast"/>  
        Lunch<form:checkbox path="Food" value="Lunch"/>  
        Dinner<form:checkbox path="Food" value="Dinner"/>  
        <br><br>  
        Leaving from: <form:select path="cityFrom">  
        <form:option value="Ghaziabad" label="Ghaziabad"/>  
        <form:option value="Modinagar" label="Modinagar"/>  
        <form:option value="Meerut" label="Meerut"/>  
        <form:option value="Amristar" label="Amristar"/>  
        </form:select>  
        <br><br>  
        Going to: <form:select path="cityTo">  
        <form:option value="Ghaziabad" label="Ghaziabad"/>  
        <form:option value="Modinagar" label="Modinagar"/>  
        <form:option value="Meerut" label="Meerut"/>  
        <form:option value="Amristar" label="Amristar"/>  
        </form:select>  
        <br><br>  
        <input type="submit" value="Submit" />  
    </form:form>  
</body>  
</html>  

confirmation-page.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<!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} <br>  
Gender: ${reservation.gender}<br>  
Meals:   
<ul>  
<c:forEach var="meal" items="${reservation.food}">  
<li>${meal}</li>  
</c:forEach>  
</ul>  
Leaving From : ${reservation.cityFrom} <br>  
Going To : ${reservation.cityTo}  
</body>  
</html>  

输出:
spring-mvc-form-drop-down-list-output1.png

spring-mvc-form-drop-down-list-output2.png

spring-mvc-form-drop-down-list-output3.png

spring-mvc-form-drop-down-list-output4.png

以上就是使用 Spring MVC 表单下拉列表创建铁路预订表单的示例。

标签: spring, Spring教程, Spring技术, Spring语言学习, Spring学习教程, Spring下载, Spring框架, Spring框架入门, Spring框架教程, Spring框架高级教程, Spring面试题, Spring笔试题, Spring编程思想