Spring MVC Notes
1 Spring MVC Introduction
1.1 What is Spring MVC?
• A web framework under the Spring Framework.
• Follows the Model-View-Controller (MVC) design pattern.
• Used for building Java-based web applications.
• Powered by Servlets and JavaServer Pages (JSP).
1.2 Key Components
• Model: Java object (POJO) used to store data.
• View: JSP file used to display results to the user.
• Controller: Java class that handles HTTP requests.
1.3 DispatcherServlet
• Acts as the front controller.
• Routes all HTTP requests to appropriate controllers.
2 Creating a Spring MVC Project
2.1 Steps to Create
• Create a Dynamic Web Project in Eclipse.
• Add Maven Nature and create pom.xml.
• Add Spring MVC dependency:
1 < dependency >
2 < groupId > org . springframework </ groupId >
3 < artifactId > spring - webmvc </ artifactId >
4 < version > 5.3.22 </ version >
5 </ dependency >
• Create folder structure:
1
– /src/main/java: Java files (Controller, Model).
– /src/main/webapp/WEB-INF: web.xml, telusko-servlet.xml.
– /views: JSP files.
• Add JSTL and Servlet dependencies if needed.
3 Running Tomcat in Eclipse
3.1 Steps
• Download and configure Apache Tomcat in Eclipse.
• Go to Servers tab → Right click → New → Server.
• Choose Tomcat version (e.g., 9.0).
• Deploy project by dragging into server.
• Start server (green play button) and access via:
1 http :// localhost :8080/ YourAppName
4 DispatcherServlet
4.1 Role
• Handles all incoming HTTP requests.
• Mapped in web.xml.
• Loads its configuration file (e.g., telusko-servlet.xml).
4.2 web.xml Configuration
1 <web - app xmlns = " http :// xmlns . jcp . org / xml / ns / javaee "
2 xmlns : xsi = " http :// www . w3 . org /2001/ XMLSchema - instance "
3 xsi : schemaLocation = " http :// xmlns . jcp . org / xml / ns / javaee
4 http :// xmlns . jcp . org / xml / ns / javaee /
web - app_3_1 . xsd "
5 version = " 3.1 " >
6
7 <! - - Register Dispat cherSe rvlet -- >
8 < servlet >
9 < servlet - name > telusko </ servlet - name >
10 < servlet - class > org . springframework . web . servlet .
DispatcherSe rvlet </ servlet - class >
11 < load - on - startup >1 </ load - on - startup >
12 </ servlet >
13
14 <! - - Servlet Mapping for all requests -- >
15 < servlet - mapping >
2
16 < servlet - name > telusko </ servlet - name >
17 <url - pattern >/ </ url - pattern >
18 </ servlet - mapping >
19 </ web - app >
Note: telusko-servlet.xml is searched inside /WEB-INF/.
5 Configuring the DispatcherServlet
5.1 telusko-servlet.xml
1 <? xml version ="1.0" encoding =" UTF -8" ? >
2 < beans xmlns = " http :// www . springframework . org / schema / beans "
3 xmlns : xsi = " http :// www . w3 . org /2001/ XMLSchema - instance "
4 xmlns : context = " http :// www . springframework . org / schema /
context "
5 xmlns : mvc = " http :// www . springframework . org / schema / mvc "
6 xsi : schemaLocation = "
7 http :// www . springframework . org / schema / beans
8 http :// www . springframework . org / schema / beans / spring - beans .
xsd
9 http :// www . springframework . org / schema / context
10 http :// www . springframework . org / schema / context / spring -
context . xsd
11 http :// www . springframework . org / schema / mvc
12 http :// www . springframework . org / schema / mvc / spring - mvc . xsd "
>
13
14 <! - - Enable annotation - based configuration -- >
15 < context : component - scan base - package = " com . telusko " / >
16 < mvc : annotation - driven / >
17
18 <! - - View Resolver -- >
19 < bean class = " org . springframework . web . servlet . view .
InternalResourceViewResolver ">
20 < property name = " prefix " value = " / WEB - INF / views / " / >
21 < property name = " suffix " value = " . jsp " / >
22 </ bean >
23 </ beans >
5.2 What it does
• Scans for @Controller classes in com.telusko.
• Enables annotations (@RequestMapping, @ModelAttribute, etc.).
• Configures view resolution: return "result" → /WEB-INF/views/result.jsp.
3
6 Internal Resource View Resolver
6.1 Purpose
• Converts logical view names into physical JSP paths.
• Example:
1 return " result " ;
→ /WEB-INF/views/result.jsp (based on prefix/suffix in config).
6.2 Configuration
1 < property name = " prefix " value = " / WEB - INF / views / " / >
2 < property name = " suffix " value = " . jsp " / >
Note: Eliminates the need to hardcode .jsp or full paths in controllers.
7 Alien.java Model Class (POJO)
7.1 Code
1 package com . telusko ;
2
3 public class Alien {
4 private int aid ;
5 private String aname ;
6
7 // Getter for aid
8 public int getAid () {
9 return aid ;
10 }
11
12 // Setter for aid
13 public void setAid ( int aid ) {
14 this . aid = aid ;
15 }
16
17 // Getter for aname
18 public String getAname () {
19 return aname ;
20 }
21
22 // Setter for aname
23 public void setAname ( String aname ) {
24 this . aname = aname ;
25 }
26
27 // Optional : toString () for debugging
28 @Override
29 public String toString () {
4
30 return " Alien [ aid = " + aid + " , aname = " + aname + " ] " ;
31 }
32 }
7.2 Purpose
• A POJO (Plain Old Java Object) used to carry data between the JSP form
and the controller.
8 HomeController.java Controller Class
8.1 Code
1 package com . telusko ;
2
3 import org . springframework . stereotype . Controller ;
4 import org . springframework . web . bind . annotation . ModelAttribute ;
5 import org . springframework . web . bind . annotation . RequestMapping ;
6
7 @Controller // Marks this class as a Spring MVC controller
8 public class HomeController {
9
10 // Maps the root URL "/" to index . jsp
11 @RequestMapping ( " / " )
12 public String home () {
13 return " index " ; // resolves to / WEB - INF / views / index . jsp
14 }
15
16 // Handles form submission from index . jsp
17 @RequestMapping ( " addAlien " )
18 public String addAlien ( @ModelAttribute ( " a1 " ) Alien a1 ) {
19 // The form data is automatically bound to the Alien
object
20 // " a1 " will be accessible in result . jsp via $ { a1 . aid } , $
{ a1 . aname }
21 return " result " ; // resolves to / WEB - INF / views / result . jsp
22 }
23 }
8.2 Purpose
• @Controller: Declares this class as a Spring MVC controller.
• @RequestMapping: Maps URLs to methods (e.g., "/" to home()).
• @ModelAttribute: Binds form data to the Alien object, accessible in result.jsp
via $a1.aid, $a1.aname.
5
9 index.jsp Input Form (View)
9.1 Code
1 <% @ page language =" java " contentType =" text / html ; charset = UTF -8"
pageEncoding =" UTF -8"% >
2 < html >
3 < head >
4 < title > Spring MVC Form </ title >
5 </ head >
6 < body >
7 <h2 > Alien Form </ h2 >
8 <! - - Submits form to "/ addAlien " -->
9 < form action =" addAlien " >
10 Enter ID : < input type =" text " name =" aid " > < br > < br >
11 Enter Name : < input type =" text " name =" aname " > < br > < br >
12 < input type =" submit " value =" Submit " >
13 </ form >
14 </ body >
15 </ html >
9.2 Purpose
• Sends form data to the addAlien handler in the controller using the GET method.
10 result.jsp Output Page (View)
10.1 Code
1 <% @ page language =" java " contentType =" text / html ; charset = UTF -8"
pageEncoding =" UTF -8"% >
2 < html >
3 < head >
4 < title > Result </ title >
5 </ head >
6 < body >
7 <h2 > Alien Details Submitted : </ h2 >
8 <! - - Accessing the Alien object passed from controller -->
9 ID : $ { a1 . aid } <br >
10 Name : $ { a1 . aname }
11 </ body >
12 </ html >
10.2 Purpose
• Displays the bound Alien model data using Expression Language ($).
11 Project Folder Structure
6
/ SpringMVCDemo
src / com / telusko
Alien . java
HomeController . java
WebContent
WEB - INF
web . xml
views
index . jsp
result . jsp
telusko - servlet . xml
12 Final Revision Notes
• Spring MVC: Java web framework based on MVC.
• DispatcherServlet: Front controller that routes requests.
• web.xml: Maps DispatcherServlet and URL patterns.
• telusko-servlet.xml: Configures controller scanning and view resolver.
• Alien.java: Model class holding form data.
• HomeController.java: Handles URL mapping and binds model.
• index.jsp: Takes user input.
• result.jsp: Displays submitted data.
• View Resolver: Maps view names (e.g., "result") to /WEB-INF/views/result.jsp.