English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring MVC form checkbox helps to select multiple options at the same time. This tag presents an HTML input tag of type checkbox.
<form:checkbox path="abc" value="element">/>
In addition to the checkbox tag, the Spring MVC form tag library also includes CheckboxTag. This tag presents multiple HTML input tags with type checkboxes. It is used only when you do not want to list all elements in the view page. In this case, you can provide elements at runtime and pass them to the tag. Since multiple options can be selected, an Array, List, or Map type element must be passed.
<form:checkboxes path="abc" items="${object.elementList}"/>
<!-- 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>
Reservation.java
package com.w3codebox; 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.w3codebox; 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
<?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>
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-扫描基础-package="com.w3codebox" /> <!--提供转换、格式化和验证支持 --> <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>
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> <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> </body> </html>
Output: