"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring MVC - Form Submission Date : 18/11/2024 (Session - 70) _____________________________________________________________________________________________________________________________ Last Session ============ * We started with Form Submission Process in Spring MVC. * Form Submission is nothing but sending data to server through Html Forms
Today Session ============= * We can collect the html form data in Request processor method in spring mvc by using annotation called @RequestParam based on Request parameter name. Example ======= @RequestMapping(value="/processEnquiryForm",method=RequestMethod.POST) public ModelAndView processEnquiryForm(@RequestParam("fullName") String name, @RequestParam("emailId") String emailId, @RequestParam("contactNo") String contactNo, @RequestParam("courses") String courseName){ ModelAndView mav = new ModelAndView("enquiry"); mav.addObject("name",fullName); .......................... .......................... .......................... .......................... } * We can collect all request parameters and store into Map Object by using @RequestParam annotation Example ======= @RequestMapping(value="/processEnquiryForm",method=RequestMethod.POST) public ModelAndView processEnquiryForm(@RequestParam Map formData){ //collecting each request parameter based on request parameter name String username = formData.get("fullName"); String emailId = formData.get("emailId"); String contactNo = formData.get("contactNo"); String courses = formData.get("courses"); ModelAndView mav = new ModelAndView("enquiry"); mav.addObject("enquiryInfo",formData); } * After collecting the request parameters in the Request processor method can we can store into Java Object for this we created Enquiry class as Java bean class Enquiry.java ============ package com.ashokit.beans; public class Enquiry { private String name; private String emailId; private String contactNo; private String courseName; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } public String getContactNo() { return contactNo; } public void setContactNo(String contactNo) { this.contactNo = contactNo; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } @Override public String toString() { return "Enquiry [name=" + name + ", emailId=" + emailId + ", contactNo=" + contactNo + ", courseName=" + courseName + "]"; } } Storing the Form Data into Enquiry Object explictly by programmer ================================================================= @RequestMapping(value="processEnquiry",method=RequestMethod.POST) public ModelAndView processEnquiry(@RequestParam Map formData) { System.out.println("Form Data:::::" + formData); //setting the form data into Java Object Enquiry enq = new Enquiry(); enq.setName(formData.get("fullName")); enq.setEmailId(formData.get("emailId")); enq.setContactNo(formData.get("contactNo")); enq.setCourseName(formData.get("courses")); ModelAndView mav = new ModelAndView("confirmation"); mav.addObject("registrationStatus", "Your Enquiry Sent To out Admin Team they will contact soon....."); //displaying the object information to view page mav.addObject("enquiry", enq); return mav; } } * If we develop the view page using Html Tags, As programmer we need to collect request parameters at server side and store that values into java object by explictly. * For normal html pages either we can request through DispatcherServlet (or) User can request webpage directly. * Spring MVC Provided tags for developing the view page of an webapplication. * Inorder to develop the view page using spring mvc provided tags first we need to include spring mvc form tags taglib in JSP Page as below <%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form"%> * After adding the taglib we can develop the view page by using below tags -----------> Similar to Html form tag -----------> Similar to Html Input tag -----------> similar to Html Select tag ----------> similar to Html Password tag ----------> Similar to Html textarea tag. * If we developed the view page using spring mvc provided tags as programmer we can't request that page directly through browser it should be rendered through DispatcherServlet. * When DispatcherServlet rendering this view page we need to bind an empty object Java Bean Class (Enquiry) to form by using form attribute called "modelAttribute". * When user submits the form automatically form data will get populated into Java bean class by DispatcherServlet. NOTE ==== * The Form Property names and Enquiry Object properties should be matched, If not matched during rendering will get an error. courseEnquiry.jsp ================= <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form"%>
Welcome To AshokIT For Course Enquiry.......

Name
Email
Contact
Course Select Java Full Stack Python Full Stack Spring Boot And Microservices Devops AWS
   
* The advantage of binding an empty Enquiry object to the form. Whenever User submitted form, the data of form will be stored into Enquiry Java bean object by the DispatcherServlet. * If we required the Enquiry Java bean object in post request method, we can get that object by using annotation called "@ModelAttribute". package com.ashokit.controller; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import com.ashokit.beans.Enquiry; @Controller public class EnquiryController { @RequestMapping("/") public ModelAndView getEnquiryPage() { ModelAndView mav = new ModelAndView("enquiryDetails"); mav.addObject("enquiryForm", new Enquiry()); return mav; } @RequestMapping(value="sendEnquiry",method = RequestMethod.POST) public ModelAndView processEnquiryDetails(@RequestParam Map formData) { ModelAndView mav = new ModelAndView("confirmation"); System.out.println("FormData::::" + formData); //creating the object For Enquiry class for storing form data Enquiry enquiry = new Enquiry(); enquiry.setName(formData.get("firstName")); enquiry.setEmailId(formData.get("emailId")); enquiry.setContactNo(formData.get("contactNo")); enquiry.setCourseName(formData.get("courseName")); //sharing the data to view page mav.addObject("enquiryDetails", enquiry); return mav; } @RequestMapping(value="sendEnquiryDetails", method=RequestMethod.POST) public ModelAndView processEnquiry(@ModelAttribute("enquiryForm") Enquiry enquiry) { ModelAndView mav = new ModelAndView("confirmation"); //sharing the data to view page mav.addObject("enquiryDetails", enquiry); return mav; } } +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++