📅  最后修改于: 2020-12-04 08:33:11             🧑  作者: Mango
Spring MVC表单复选框有助于同时选择多个选项。此标记呈现类型为checkbox的HTML输入标记。
除了复选框标签之外,Spring MVC表单标签库还包含复选框标签。此标签呈现带有类型复选框的多个HTML输入标签。仅当您不想列出视图页面中的所有元素时,才使用此标记。在这种情况下,您可以在运行时提供元素并将其传递给标签。由于用户可以选择多个选项,因此您需要传递Array,List或Map类型的元素。
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
Reservation.java
package com.javatpoint;
public class Reservation {
private String firstName;
private String lastName;
private String Gender;
private String[] Food;
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;
}
}
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";
}
}
web.xml
SpringMVC
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
spring-servlet.xml
index.jsp
Railway Registration Form
Click here for reservation.
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
确认页面.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}
输出: