0% found this document useful (0 votes)
1 views40 pages

1.3 Handling an HTML Form in Spring MVC - Final

The document outlines the process of handling HTML forms in a Spring MVC web application, detailing how to manage GET and POST requests using the @RequestParam annotation. It describes the creation of a StudentAdmissionController class, along with JSP files for form input and success confirmation. The document also explains how to retrieve form data and handle default values using @RequestParam, as well as the role of the Front Controller and ViewResolver in processing requests and generating responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views40 pages

1.3 Handling an HTML Form in Spring MVC - Final

The document outlines the process of handling HTML forms in a Spring MVC web application, detailing how to manage GET and POST requests using the @RequestParam annotation. It describes the creation of a StudentAdmissionController class, along with JSP files for form input and success confirmation. The document also explains how to retrieve form data and handle default values using @RequestParam, as well as the role of the Front Controller and ViewResolver in processing requests and generating responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

www.techmentor.co.

in
Spring MVC Web App with Form Handling 2

05/05/25
Spring MVC Web App with Form Handling 3

05/05/25
Simple Spring MVC Web App

Now nothing official about Handling the HTML


Form in our MVC Application.

Now we will see ---------- how we can handle get


and post requests using Spring MVC Framework.

Here we will see a vital use of @RequestParam


Annotation.

Spring MVC Web App with Form Handling 4

05/05/25
Simple Spring MVC Web App

In turn ---------- we will see how to handle HTML


Form in our Spring MVC Application

Just to save some time I have already created


some ultra simple codes which we will discuss
during our session

Let us first discuss ----------- What this whole


application is all about ------------------
Spring MVC Web App with Form Handling 5

05/05/25
Simple Spring MVC Web App

When we run this application on server ------------

The client will enter an URL on browser.

And it will get following response

Spring MVC Web App with Form Handling 6

05/05/25
Spring MVC Web App with Form Handling 7

05/05/25
Simple Spring MVC Web App
Our Spring MVC application will send a form to a
client and subsequently the client will fill up the form
with all the required values.
And then it will submit the form back to the
application.
Once this data is received by the Spring MVC
Application on the server ---------- it will process the
data and generates a response in terms of
confirmation message.
Spring MVC Web App with Form Handling 8

05/05/25
Simple Spring MVC Web App

Now these are simple steps to follow

Creating a StudentAdmissionController.Java class


Creating AdmissionForm.JSP

Creating AdmissionSuccess.JSP.

So first create the said Controller Class.

This class will have two methods

Spring MVC Web App with Form Handling 9

05/05/25
Simple Spring MVC Web App

This class will have two methods


getAdmissionForm()

submitAdmissionForm()

The application will also have 2 JSPs


AdmissionFrom.JSP

AdmissionSuccess.JSP

Spring MVC Web App with Form Handling 10

05/05/25
Simple Spring MVC Web App

When the client will enter and hit following URL


----------------

The request will reach to this application

And here the Front Controller of this application


will make a call to this method in Controller Class.

Spring MVC Web App with Form Handling 11

05/05/25
Simple Spring MVC Web App

But importantly ----------- how it will decide to


make a call to this method only amongst all
other methods which are present in this
application ??

Simply --------- By analyzing two things for this


incoming URL request ----

First thing ---------- Off Course ------ the pattern


Spring MVC Web App with Form Handling 12

05/05/25
Simple Spring MVC Web App

First thing ---------- Off Course ------ the pattern

Of this highlighted URL -------------------

If the pattern of this URL is matching with the


pattern specified in @RequestMapping
Annotation ----------------- it meets first criteria
Spring MVC Web App with Form Handling 13

05/05/25
Simple Spring MVC Web App

Now what is that extra line of code followed by


the pattern ???????

i.e. method = RequestMethod.GET


This is talking about the HTTP Request which client has
made.

We already have an idea about HTTP Get / Post

Spring MVC Web App with Form Handling 14

05/05/25
Simple Spring MVC Web App

So here ------------ When a client makes a request


to the application ----------- using this URL

That request can be of get type or post type

So when we write method = RequestMethod.GET


statement here -----------
It simply means that this method getAdmissionForm()
can process the requests which are of type get.
Spring MVC Web App with Form Handling 15

05/05/25
Simple Spring MVC Web App

When a client makes a request to the application


----------- using this URL

The browser by default makes a get request to a


server.

When this request hits our application ------------


the Front Controller receives it and makes a call
to the method getAdmissionForm()
Spring MVC Web App with Form Handling 16

05/05/25
Simple Spring MVC Web App

Here the matching comparison factors are ---


URL pattern ------- value="/admissionForm.html"

Request Method ------- method = RequestMethod.GET

So overall header of the method which receives a call


from Front Controller is as follows
@RequestMapping(value="/admissionForm.html", method = RequestMethod.GET )

public ModelAndView getAdmissionForm() {

ModelAndView model = new ModelAndView("AdmissionForm");

return model }
Spring MVC Web App with Form Handling 17

05/05/25
Simple Spring MVC Web App

This method getAdmissionForm() is simply


returning ModelAndView object

Having view details “AdmissionForm” ----------


back to the front controller

The Front Controller straight away forwards


the request to the AdmissionForm.JSP

How ??????????
Spring MVC Web App with Form Handling 18

05/05/25
Simple Spring MVC Web App

How ??????????

This is done by the ViewResolver class and its


prefix and suffix properties

Spring MVC Web App with Form Handling 19

05/05/25
Simple Spring MVC Web App

Now when control reaches here in


AdmissionForm.JSP

Spring MVC Web App with Form Handling 20

05/05/25
Simple Spring MVC Web App

Now when client will submit this form on the


browser -----------

The request type which would reach the


application will be a POST type.

So the parameters for response creation will be


taken from this form.

Spring MVC Web App with Form Handling 21

05/05/25
Simple Spring MVC Web App

So next URL to be processed is as follows -----------

Why this is POST ???????

Because post method specified in the form and


the URL specified in the Action attribute.

And then further Front Controller will now make


a call to a method submitAdmissionForm().
Spring MVC Web App with Form Handling 22

05/05/25
Simple Spring MVC Web App

Now --------------- Importantly ------------- When


client submits this “AdmissionForm.JSP” form on
the browser -------------------- it will send values of
2 request parameters
studentName

studentHobby

This will reach up to the application via request.


Spring MVC Web App with Form Handling 23

05/05/25
Simple Spring MVC Web App

Now --------------- Most Importantly -------------


When client submits this “AdmissionForm.JSP”
form on the browser -------------------- it will send
values of 2 request parameters
studentName

studentHobby

This will reach up to the application via request.


Spring MVC Web App with Form Handling 24

05/05/25
Simple Spring MVC Web App

Now --------------- When the front controller --------


makes a call to second method i.e.
submitAdmissionForm() ------------

Here the @RequestParam annotation used in the


method would simply retrieve the values of
studentName and studentHobby (i.e. request
parameters from request) in body of method
Spring MVC Web App with Form Handling 25

05/05/25
Simple Spring MVC Web App

Here ----------- @RequestParam annotation will


simply bind the value of studentName with the
string type name variable.

Same will happen with student Hobby.

So further ------------------- in the method ----------


we can use --------- these values anywhere
---------- we want
Spring MVC Web App with Form Handling 26

05/05/25
Simple Spring MVC Web App

The method submitAdmissionForm() here


------------ is simply ----------- returning a
ModelAndView object back to the front
controller ---------------

Having view details --------------- as


“AdmissionSuccess” --------------- and the
message which has to be printed -------------- in
the response web page
Spring MVC Web App with Form Handling 27

05/05/25
Simple Spring MVC Web App

So once it prepares and submits ModelAndView


object back to Front Controller ----------------
Now the Front Controller ------------ as usual
---------------- uses the ViewResolver
And by using -------- View Name -------- prefix
-------- suffix values -----------------
Front Controller will forward the request to
Spring MVC Web App with Form Handling 28

AdmissionSuccess.JSP
05/05/25
Simple Spring MVC Web App

Off Course ----------------- Front Controller does


this --------------- with the help of view resolver
class.

And finally ------------------ the final response is


built in terms of --------------------
AdmissionSuccess.JSP ---------

Spring MVC Web App with Form Handling 29

05/05/25
Simple Spring MVC Web App

This JSP file is simply putting ------------------ the


Message value ------------------

Which has been set by the second method in the


controller ---------------- i.e.
submitAdmissionForm() ----------------

It may also have other HTML code.

Spring MVC Web App with Form Handling 30

05/05/25
Simple Spring MVC Web App

Now this response is sent back to Front


Controller ----------------------

And the Front Controller ---------------------- in turn


will send in to the client – browser.

Spring MVC Web App with Form Handling 31

05/05/25
Simple Spring MVC Web App

Some typical aspects of @RequestParam


Annotation ----------------

Here --------- when client submitted a form


----------- with may be --------- Sachin as a Name
---------- and music as a Hobby -------

Our application returned same values in the


response.
Spring MVC Web App with Form Handling 32

05/05/25
Simple Spring MVC Web App

Now what if the client ---------------- does not


provide the vaue of any parameter ???????

Our application will send back an empty string


back in the response.

What if --------------- I want to give some default


value to a Form Parameter ???????????

Spring MVC Web App with Form Handling 33

05/05/25
Simple Spring MVC Web App

We can do this mechanism --------------- for


default parameter ---------------------- by just
adding one more @RequestParam parameter
--------
@RequestMapping(value="/submitAdmissionForm.html", method =
RequestMethod.POST)

public ModelAndView submitAdmissionForm(@RequestParam(value =


“studentName”, defaultValue = “Mr. Bond” ) String name ,
@RequestParam(-----------------------------)
Spring MVC Web App with Form Handling 34

05/05/25
Simple Spring MVC Web App

We can do this mechanism --------------- for


default parameter ---------------------- by just
adding one more @RequestParam parameter
property
@RequestMapping(value="/submitAdmissionForm.html", method =
RequestMethod.POST)

public ModelAndView submitAdmissionForm(@RequestParam(value =


“studentName”, defaultValue = “Mr. Bond” ) String name ,
@RequestParam(-----------------------------)
Spring MVC Web App with Form Handling 35

05/05/25
Simple Spring MVC Web App

One more point -------------- regarding


@RequestParam Annotation -----------

Instead of specifying these multiple


@RequestParam Annotations ---------------

We can use only one @RequestParam Annotation


on a Map.

We have already done this in case @PathVariable


Spring MVC Web App with Form Handling 36

05/05/25
Simple Spring MVC Web App
@RequestMapping(value="/submitAdmissionForm.html", method =
RequestMethod.POST)
public ModelAndView submitAdmissionForm(@RequestParam
Map<String,String>
reqPar) {
String name = reqPar.get("studentName");
String hobby = reqPar.get("hobby");

ModelAndView model = new ModelAndView("AdmissionSuccess");


model.addObject("msg","Details submitted by you:: Name: "+name+ ",
Hobby: " + hobby);
return model;
} MVC Web App with Form Handling
Spring 37

05/05/25
Simple Spring MVC Web App

One more point -------------- regarding


@RequestParam Annotation -----------

Instead of specifying these multiple


@RequestParam Annotations ---------------

We can use only one @RequestParam Annotation


on a Map.

Spring MVC Web App with Form Handling 38

05/05/25
Spring MVC Web App with Form Handling 39

05/05/25
One more point -------------- regarding
@RequestParam Annotation -----------

Instead of specifying these multiple


@RequestParam Annotations ---------------

We can use only one @RequestParam Annotation


on a Map.

Spring MVC Web App with Form Handling 40

05/05/25

You might also like