📜  Spring MVC表单下拉列表

📅  最后修改于: 2020-12-04 08:34:36             🧑  作者: Mango

Spring MVC表单下拉列表

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

句法


这是用于选择选项的其他一些标签。

选项标签

该标签生成HTML选项标签。每个标签都包含一个可由用户选择的值。

句法


选项标签

该标签生成HTML选项标签的列表。每个标签包含用户选择的元素列表。

句法


Spring MVC表单下拉列表示例

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

          

    org.springframework
    spring-webmvc
    5.1.1.RELEASE


    
  
    javax.servlet  
    servlet-api  
    3.0-alpha-1  


    

    javax.servlet
    jstl
    1.2



    org.apache.tomcat
    tomcat-jasper
    9.0.12

2.创建bean类

Reservation.java

package com.javatpoint;

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 com.javatpoint;
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



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

5.在xml文件中定义bean

spring-servlet.xml



    
    
    
    
    
     
        
             
     

6.创建请求的页面

index.jsp




    Railway Registration Form


Click here for reservation.


7.创建视图组件

Reservation-page.jsp

<%@ 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

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



Your reservation is confirmed successfully. Please, re-check the details.

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

输出: