Sunday, September 15, 2019

Spring MVC Form Checkbox

The Spring MVC form checkbox facilitates to choose multiple options at the same time. This tag renders an HTML input tag of type checkbox.

Syntax

  1. <form:checkbox path="abc" value="element"/>  
Apart from checkbox tag, Spring MVC form tag library also contains checkboxes tag. This tag renders multiple HTML input tags with type checkbox. This tag is used only if you don't want to list all the elements in the view page. In such a case, you can provide the elements at runtime and pass it to the tag. As the user can select multiple options, you need to pass the elements of Array, a List or a Map type.

Syntax

  1. <form:checkboxes path="abc" items="${object.elementList}"/>  

Example of Spring MVC Form Check Box

1. Add dependencies to pom.xml file.

  1.           <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8.     <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  9. <dependency>    
  10.     <groupId>javax.servlet</groupId>    
  11.     <artifactId>servlet-api</artifactId>    
  12.     <version>3.0-alpha-1</version>    
  13. </dependency>  
  14.   
  15.     <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
  16. <dependency>  
  17.     <groupId>javax.servlet</groupId>  
  18.     <artifactId>jstl</artifactId>  
  19.     <version>1.2</version>  
  20. </dependency>  
  21. <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
  22. <dependency>  
  23.     <groupId>org.apache.tomcat</groupId>  
  24.     <artifactId>tomcat-jasper</artifactId>  
  25.     <version>9.0.12</version>  
  26. </dependency>  

2. Create the bean class

Reservation.java

  1. package com.javatpoint;  
  2.   
  3. public class Reservation {  
  4.   
  5.     private String firstName;  
  6.     private String lastName;  
  7.     private String Gender;  
  8.     private String[] Food;  
  9.     public Reservation()  
  10.     {         
  11.     }  
  12.     public String getFirstName() {  
  13.         return firstName;  
  14.     }  
  15.     public void setFirstName(String firstName) {  
  16.         this.firstName = firstName;  
  17.     }  
  18.     public String getLastName() {  
  19.         return lastName;  
  20.     }  
  21.     public void setLastName(String lastName) {  
  22.         this.lastName = lastName;  
  23.     }  
  24.     public String getGender() {  
  25.         return Gender;  
  26.     }  
  27.     public void setGender(String gender) {  
  28.         Gender = gender;  
  29.     }  
  30.     public String[] getFood() {  
  31.         return Food;  
  32.     }  
  33.     public void setFood(String[] food) {  
  34.         Food = food;  
  35.     }         
  36. }  

3. Create the controller class

ReservationController.java
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.web.bind.annotation.ModelAttribute;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. @RequestMapping("/reservation")  
  8. @Controller  
  9. public class ReservationController {  
  10.     @RequestMapping("/bookingForm")  
  11. public String bookingForm(Model model)  
  12. {  
  13.       //create a reservation object   
  14.     Reservation res=new Reservation();  
  15.       //provide reservation object to the model   
  16.     model.addAttribute("reservation", res);  
  17.     return "reservation-page";  
  18. }  
  19. @RequestMapping("/submitForm")  
  20. public String submitForm(@ModelAttribute("reservation") Reservation res)  
  21. {  
  22.     return "confirmation-form";  
  23. }  
  24. }  

4. Provide the entry of controller in the web.xml file

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>SpringMVC</display-name>  
  4.    <servlet>    
  5.     <servlet-name>spring</servlet-name>    
  6.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  7.     <load-on-startup>1</load-on-startup>      
  8. </servlet>    
  9. <servlet-mapping>    
  10.     <servlet-name>spring</servlet-name>    
  11.     <url-pattern>/</url-pattern>    
  12. </servlet-mapping>    
  13. </web-app>  

5. Define the bean in the xml file

spring-servlet.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.     <!-- Provide support for component scanning -->  
  14.     <context:component-scan base-package="com.javatpoint" />  
  15.     <!--Provide support for conversion, formatting and validation -->  
  16.     <mvc:annotation-driven/>  
  17.     <!-- Define Spring MVC view resolver -->  
  18.      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  19.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  20.         <property name="suffix" value=".jsp"></property>       
  21.      </bean>  
  22. </beans>  

6. Create the requested page

index.jsp
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Railway Registration Form</title>  
  5. </head>  
  6. <body>  
  7. <a href="reservation/bookingForm">Click here for reservation.</a>  
  8. </body>  
  9. </html>  

7. Create the view components

reservation-page.jsp
  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head>  
  5.     <title>Reservation Form</title>  
  6. </head>  
  7. <h3>Railway Reservation Form</h3>  
  8. <body>  
  9.     <form:form action="submitForm" modelAttribute="reservation">  
  10.         First name: <form:input path="firstName" />         
  11.         <br><br>  
  12.         Last name: <form:input path="lastName" />  
  13.         <br><br>  
  14.         Gender:   
  15.         Male<form:radiobutton path="Gender" value="Male"/>  
  16.         Female<form:radiobutton path="Gender" value="Female"/>  
  17.         <br><br>  
  18.         Meals:  
  19.         BreakFast<form:checkbox path="Food" value="BreakFast"/>  
  20.         Lunch<form:checkbox path="Food" value="Lunch"/>  
  21.         Dinner<form:checkbox path="Food" value="Dinner"/>  
  22.         <br><br>  
  23.         <input type="submit" value="Submit" />  
  24.     </form:form>  
  25. </body>  
  26. </html>  
confirmation-page.jsp
  1. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <body>  
  5. <p>Your reservation is confirmed successfully. Please, re-check the details.</p>  
  6. First Name : ${reservation.firstName} <br>  
  7. Last Name : ${reservation.lastName} <br>  
  8. Gender: ${reservation.gender}<br>  
  9. Meals:   
  10. <ul>  
  11. <c:forEach var="meal" items="${reservation.food}">  
  12. <li>${meal}</li>  
  13. </c:forEach>  
  14. </ul>  
  15. </body>  
  16. </html>  
Output:
Spring MVC Form Checkbox
Spring MVC Form Checkbox
Spring MVC Form Checkbox
Spring MVC Form Checkbox
























No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...