Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaSpring MVC – Capture and Display the Data from Registration Form

Spring MVC – Capture and Display the Data from Registration Form

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. 

Spring MVC - Capture and Display the Data from Registration Form

 

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.neveropen.calculator.controllers;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.neveropen.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.

 

RELATED ARTICLES

Most Popular

Recent Comments