📜  Spring – MVC 列表框

📅  最后修改于: 2022-05-13 01:55:04.933000             🧑  作者: Mango

Spring – MVC 列表框

Spring Web MVC 框架演示如何在表单中使用 Listbox。让我们首先设置一个 Eclipse IDE,然后按照步骤使用 Spring Web 框架创建一个基于动态表单的 Web 应用程序。

Spring -MVC 框架

这些项目在 Spring MVC 表单列表框中列出。此标记在 HTML 中创建一个选择元素。它允许您将数据绑定到您选择的元素。

句法:

  

这里还有一些可用于缩小选择范围的标签。

A. 选项标签: HTML 选项标签就是由这个标签生成的。每个标签都有一个用户可以选择的值。

  

B. 选项标签:由该标签生成一个 HTML 选项标签列表。每个标签都有一个用户选择的组件列表。

  

Spring MVC – 列表框

项目结构如下:

执行:

第 1 步:将依赖项添加到 pom.xml 文件。

文件:pom.xml

XML

  4.0.0
  com.geeksforgeeks
  SpringMVC
  war
  0.0.1-SNAPSHOT
  SpringMVC Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
      
    

    org.springframework
    spring-webmvc
    5.1.1.RELEASE

  


    org.apache.tomcat
    tomcat-jasper
    9.0.12

    
  
    javax.servlet  
    servlet-api  
    3.0-alpha-1  

  


    javax.servlet
    jstl
    1.2

  
  
  
    SpringMVC
  


Java
// Java Program to Illustrate Reservation Class
  
package com.geeksforgeeks;
  
// Class
public class Reservation {
    // Class data members
    private String firstName;
    private String lastName;
    private String Gender;
    private String[] Food;
    private String cityFrom;
    private String cityTo;
  
    // Constructor
    public Reservation() {}
  
    // Getters and Setters
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName)
    {
        // this keyword refers to current instance itself
        this.firstName = firstName;
    }
  
    // Getters and Setters
    public String getLastName() { return lastName; }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
  
    // Getters and Setters
    public String getGender() { return Gender; }
    public void setGender(String gender)
    {
        Gender = gender;
    }
  
    // Getters and Setters
    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;
    }
  
    // Getters and Setters
    public String getCityTo() { return cityTo; }
    public void setCityTo(String cityTo)
    {
        this.cityTo = cityTo;
    }
}


Java
// Java Program to Illustrate ReservationController Class
  
package com.geeksforgeeks;
  
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
  
// Annotation
@RequestMapping("/reservation")
@Controller
  
// Class
public class ReservationController {
    @RequestMapping("/bookingForm")
  
    // Method
    public String bookingForm(Model model)
    {
        Reservation res = new Reservation();
        model.addAttribute("reservation", res);
  
        return "reservation-page";
    }
  
    // Annotation
    @RequestMapping("/submitForm")
    // Method
    public String submitForm(@ModelAttribute("reservation")
                             Reservation res)
    {
        return "confirmation-form";
    }
}


Java


  SpringMVC
     
    spring  
    org.springframework.web.servlet.DispatcherServlet  
    1    
  
  
    spring  
    /  
  


XML


  
    
    
      
    
    
     
        
               
     


HTML



    Railway Reservation System


GFG Railway Reservation System.



HTML
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>



    Reservation Form

Railway Reservation Form

             First name:                  

        Last name:         

        Gender:          Male         Female         

        Meals:         BreakFast         Lunch         Dinner         

        Leaving from:                                             

        Going to:                                             

             


HTML
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



  

Geeksforgeeks reservation is confirmed successfully.

   First Name : ${reservation.firstName}
Last Name : ${reservation.lastName}
Gender: ${reservation.gender}
Meals: 
  • ${meal}
Leaving From : ${reservation.cityFrom}
Going To : ${reservation.cityTo}


第 2 步:创建 bean 类

资料:预订。Java

Java

// Java Program to Illustrate Reservation Class
  
package com.geeksforgeeks;
  
// Class
public class Reservation {
    // Class data members
    private String firstName;
    private String lastName;
    private String Gender;
    private String[] Food;
    private String cityFrom;
    private String cityTo;
  
    // Constructor
    public Reservation() {}
  
    // Getters and Setters
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName)
    {
        // this keyword refers to current instance itself
        this.firstName = firstName;
    }
  
    // Getters and Setters
    public String getLastName() { return lastName; }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
  
    // Getters and Setters
    public String getGender() { return Gender; }
    public void setGender(String gender)
    {
        Gender = gender;
    }
  
    // Getters and Setters
    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;
    }
  
    // Getters and Setters
    public String getCityTo() { return cityTo; }
    public void setCityTo(String cityTo)
    {
        this.cityTo = cityTo;
    }
}

第三步:创建控制器类

文件:预订控制器。Java

Java

// Java Program to Illustrate ReservationController Class
  
package com.geeksforgeeks;
  
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
  
// Annotation
@RequestMapping("/reservation")
@Controller
  
// Class
public class ReservationController {
    @RequestMapping("/bookingForm")
  
    // Method
    public String bookingForm(Model model)
    {
        Reservation res = new Reservation();
        model.addAttribute("reservation", res);
  
        return "reservation-page";
    }
  
    // Annotation
    @RequestMapping("/submitForm")
    // Method
    public String submitForm(@ModelAttribute("reservation")
                             Reservation res)
    {
        return "confirmation-form";
    }
}

第四步:在 web.xml 文件中提供控制器的入口

文件:web.xml

Java



  SpringMVC
     
    spring  
    org.springframework.web.servlet.DispatcherServlet  
    1    
  
  
    spring  
    /  
  

第 5 步:在 XML 文件中定义 bean

文件:spring-servlet.xml

XML



  
    
    
      
    
    
     
        
               
     

第 6 步:创建请求的页面

文件:index.jsp

HTML




    Railway Reservation System


GFG Railway Reservation System.


第 7 步:创建视图组件

文件:reservation-page.jsp

HTML

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>



    Reservation Form

Railway Reservation Form

             First name:                  

        Last name:         

        Gender:          Male         Female         

        Meals:         BreakFast         Lunch         Dinner         

        Leaving from:                                             

        Going to:                                             

             

文件:确认页面.jsp

HTML

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



  

Geeksforgeeks reservation is confirmed successfully.

   First Name : ${reservation.firstName}
Last Name : ${reservation.lastName}
Gender: ${reservation.gender}
Meals: 
  • ${meal}
Leaving From : ${reservation.cityFrom}
Going To : ${reservation.cityTo}

输出:

单击链接,您将看到以下输出

选择其他列表框

最后将显示此输出