			 	             "Welcome To Ashok IT"				
			                  "Spring Boot and MicroServices"
                                            Topic : Spring MVC - Form Submission
                                            Date  : 13/05/2025
                                              (Session - 45)                               
_____________________________________________________________________________________________________________________________
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<String,String> 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<String,String> 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

       <spring:form>   -----------> Similar to Html form tag

       <spring:input>  -----------> Similar to Html Input tag

       <spring:select> -----------> similar to Html Select tag

       <spring:password> ----------> similar to Html Password tag

       <spring:textarea> ----------> 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"%>
<!DOCTYPE html>
<html>

<body bgcolor="cyan">

	<div style='text-align: center; color: red;'>Welcome To AshokIT For Course Enquiry.......</div>
	<br />
	<div>
		<spring:form action="processEnquiry" method="post"
			modelAttribute="enquiryForm">
			<table align="center">
				<tr>
					<td>Name</td>
					<td><spring:input path="fullName" /></td>
				</tr>
				<tr>
					<td>Email</td>
					<td><spring:input path="emailId" /></td>
				</tr>
				<tr>
					<td>Contact</td>
					<td><spring:input path="contactNo" /></td>
				</tr>
				<tr>
					<td>Course</td>
					<td><spring:select path="courseName">
				               <spring:option value="">Select</spring:option>
					       <spring:option value="JavaFullStack">Java Full Stack</spring:option>
					       <spring:option value="PythonFullStack">Python Full Stack</spring:option>
					<spring:option value="SpringBootWithMS">Spring Boot And Microservices</spring:option>
					       <spring:option value="Devops">Devops</spring:option>
					       <spring:option value="AWS">AWS</spring:option>
					    </spring:select>
                                        </td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td><spring:button value="submit" /></td>
					<td><spring:button value="cancel" /></td>
				</tr>
			</table>
		</spring:form>
	</div>
</body>
</html>

* 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<String,String> 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;
	}
}

Differences between Html Tags Vs Spring MVC Tags
===================================================
Html Tags
=========
1) These tags are given by W3C (World Wide Web Consortium).

2) These tags are support only one way of data binding when we are used in spring mvc application development.
                       (form -----------> Model Class Object) 

3) When we are working Html form tag the default http request method is "GET".

4) Html Tags are not case-sensitive.

5) We can use the Html Tags inside JSP Page(Html tags,JSP Tags).

6) While using Html Tags in webpage we are not writing any kind taglib statement.

7) While submitting form data to server Here DispatcherServlet will not store form data into Java Object implictly.


Spring MVC Tags
===============
1) These tags are given by Spring MVC Framework(Pivotal Team)

2) These tags are supporting two way data binding when we are used in spring mvc application development.

       (form ----------> model class object , model class object ------------> form)

3) When we are working Spring MVC provided form tag the default http request method is "POST".

4) Spring MVC Provided tags are much case-sensitive.

5) We can used only in the JSP Pages.

6) While using Spring MVC Provided Tags definetly need to write taglib statement at top of JSP Page.

7) While submitting form data to server Here DispatcherServlet will store form data into Java object implictly.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++