Spring MVC - Capture and Display the Data from Registration Form
Last Updated : 30 May, 2022
This article is the continuation of this article Spring MVC - Create Registration Form using Form Tag Library, where we have successfully created a registration form using the Form Tag Library. Here in this article, we are going to explain how can we capture the data that are entered by the user and display it on our next page after clicking the Register button. A sample image is given below to get an idea about what we are going to do in this article.
Implementation
Add Code in the registration-page.jsp file
Go to the registration-page.jsp file and add the below line of code inside the form:form tag.
<form:form action="registration-complete" method="get" modelAttribute="userRegInfo">
All other things remain unchanged.
File: Updated registration-page.jsp
HTML <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <body> <h1 align="center">Create Registration Form using Form Tag in Spring MVC</h1> <!-- Changes in this line --> <form:form action="registration-complete" method="get" modelAttribute="userRegInfo"> <div align="center"> <!-- A Simple Input Field --> <label>Name : </label> <form:input path="name"/> <br/> <label>User Name : </label> <form:input path="userName"/> <br/> <label>Password : </label> <form:password path="password"/> <br/> <!-- DropDown Field --> <label>Branch : </label> <form:select path="branch"> <form:option value="CSE" label="Computer Science"></form:option> <form:option value="CSA" label="Computer Science and Application"></form:option> <form:option value="EE" label="Electrical Engineering"></form:option> <form:option value="ME" label="Mechanical Engineering"></form:option> </form:select> <br/> <!-- CheckBox Field --> <label>Skills : </label> Java : <form:checkbox path="skills" value="java"/> Python : <form:checkbox path="skills" value="python"/> C++ : <form:checkbox path="skills" value="cpp"/> DSA : <form:checkbox path="skills" value="dsa"/> Spring : <form:checkbox path="skills" value="spring"/> <br/> <!-- RadioButton Field --> <label>Gender : </label> Male<form:radiobutton path="gender" value="male"/> Female<form:radiobutton path="gender" value="female"/> <br/> <!-- Button Field --> <input type="submit" value="Register"> </div> </form:form> </body> </html>
Add Code in RegistrationController.java File
Now again come to the RegistrationController.java file and create another method something like this with the "registration-complete" endpoint because we have mentioned the same inside the form:form tag as an action.
@RequestMapping("/registration-complete") public String processUserReg(@ModelAttribute("userRegInfo") UserRegistrationDTO userRegistrationDTO) { return "registration-complete"; }
File: Updated RegistrationController.java
Java package com.geeksforgeeks.calculator.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.geeksforgeeks.calculator.dto.UserRegistrationDTO; @Controller public class RegistrationController { @RequestMapping("/register") public String showRegistrationPage(@ModelAttribute("userRegInfo") UserRegistrationDTO userRegistrationDTO) { return "registration-page"; } @RequestMapping("/registration-complete") public String processUserReg(@ModelAttribute("userRegInfo") UserRegistrationDTO userRegistrationDTO) { return "registration-complete"; } }
Create a New View
In the next step, we have to create a new view named "registration-complete" inside the WEB-INF > view folder. And below is the code for the registration-complete.jsp file.
HTML <html> <head> </head> <body> <h1 align="center">Registration Successful</h1> <h2>The details entered by the user are :</h2> Name: ${userRegInfo.name} <br/> User Name: ${userRegInfo.userName} <br/> Password: ${userRegInfo.password} <br/> Branch: ${userRegInfo.branch} <br/> Skills: ${userRegInfo.skills} <br/> Gender: ${userRegInfo.gender} <br/> </body> </html>
So we are done with the coding. Now, let's run our application again and test if things are working fine or not.
Output
Hit the following URL to run your controller
http://localhost:8080/simple-calculator/geeksforgeeks.org/register
Output:
Let's fill the form,
Then click on the "Register" button and you can see all the details that are entered by the user have been displayed successfully.
Similar Reads
Spring MVC JSTL Configuration JavaServer Pages Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. Here we will be discussing how to use the Maven build tool to add JSTL support to a Spring MVC application. also, you'll learn how to act
1 min read
Spring MVC with MySQL - Sample Project For Calculating Electricity Bill Let us see a sample electricity bill calculation project by using Spring MVC + MySQL connectivity + JDBCTemplate. Additionally, let us test the same by using MockMvc + JUnit. MySQL Queries: DROP DATABASE IF EXISTS test; CREATE DATABASE test; USE test; DROP TABLE test.personsdetails; CREATE TABLE per
7 min read
Spring MVC - Comparison of Cryptocurrencies using REST API REST APIS is available in plenty nowadays. As cryptocurrencies are a hot topic nowadays, there is always a need to compare the different cryptocurrencies and get the corresponding value in different currencies. As a sample, let us take a REST API call as https://min-api.cryptocompare.com/data/price?
6 min read
Spring MVC - Get Probability of a Gender by Providing a Name using REST API A lot of funful REST API calls are available as open source. Suppose if we like to keep a name to our nears and dears, we can just check that by means of a REST API call and get the gender, what is the probability of being that gender and how many times does it come with it? Relevant REST API call h
7 min read
Get Time Zone by Providing Latitude and Longitude using Spring MVC and REST API Spring MVC Framework follows the Model-View-Controller design pattern. It is used to develop web applications. It works around DispatcherServlet. DispatcherServlet handles all the HTTP requests and responses. In this article, we are going to see about a REST API call to find the coordinates for the
6 min read
Spring MVC with MySQL and Junit - Finding Employees Based on Location In real-world scenarios, organizations are existing in different localities. Employees are available in many locations. Sometimes they work in different 2 locations i.e. for a few days, they work on location 1 and for a few other days, they work on location 2. Let's simulate this scenario via MySQL
8 min read
Spring MVC - Get University/College Details via REST API REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing. Spring MVC is a Web MVC Framework for building web applicat
6 min read
Spring MVC - JSTL forEach Tag with Example JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Spring MVC - Iterating List on JSP using JSTL JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Two-Way Data Binding in Spring MVC with Example Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
7 min read