Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views17 pages

Spring MVC Student Management System

The document outlines a guide for building a Student Management System using Spring MVC, JDBC, MySQL, and Postman. It includes detailed phases for setting up the project, creating the database, building the application architecture, and testing the application. The guide is structured with step-by-step instructions for each phase, making it an interactive coding adventure for learners.

Uploaded by

mr.uthej11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views17 pages

Spring MVC Student Management System

The document outlines a guide for building a Student Management System using Spring MVC, JDBC, MySQL, and Postman. It includes detailed phases for setting up the project, creating the database, building the application architecture, and testing the application. The guide is structured with step-by-step instructions for each phase, making it an interactive coding adventure for learners.

Uploaded by

mr.uthej11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Spring MVC Student Management

System
Your Fun & Interactive Coding Adventure!

What You’ll Build:


A complete web application for managing student
records
with Spring MVC, JDBC, MySQL, and Postman
testing

Author: Your Coding Mentor

May 22, 2025


Spring MVC Student Management Guide

Contents

1 What You’ll Build 2

2 Your Coding Toolkit 2

3 Phase 1: Setting Up Your Digital Workshop 2


3.1 Step 1: Create Your Maven Project in Eclipse . . . . . . . . . . . . . . . 2
3.2 Step 2: Configure Your pom.xml (The Recipe Book) . . . . . . . . . . . 3

4 Phase 2: Building Your Project Architecture 4


4.1 Step 3: Create Your Folder Structure . . . . . . . . . . . . . . . . . . . . 4
4.2 Step 4: Create the Student Model (Your Data Blueprint) . . . . . . . . . 4

5 Phase 3: Database Magic 5


5.1 Step 5: Create Your MySQL Database . . . . . . . . . . . . . . . . . . . 5
5.2 Step 6: Create the DAO Interface (Your Database Contract) . . . . . . . 6
5.3 Step 7: Implement the DAO (Your Database Worker) . . . . . . . . . . . 6

6 Phase 4: Creating Your Web Controllers 8


6.1 Step 8: Build the Student Controller (Your Traffic Director) . . . . . . . 8

7 Phase 5: Configuration Magic 9


7.1 Step 9: Create Database Configuration . . . . . . . . . . . . . . . . . . . 9
7.2 Step 10: Create Spring MVC Configuration . . . . . . . . . . . . . . . . 10

8 Phase 6: Creating Beautiful Web Pages 11


8.1 Step 11: Create Student List Page . . . . . . . . . . . . . . . . . . . . . . 11

9 Phase 7: Testing with Postman 12


9.1 Step 12: Create REST Controller for API Testing . . . . . . . . . . . . . 12
9.2 Step 13: Postman Testing Guide . . . . . . . . . . . . . . . . . . . . . . . 13

10 Phase 8: Launch Your Application 14


10.1 Step 14: Configure Web.xml . . . . . . . . . . . . . . . . . . . . . . . . . 14
10.2 Step 15: Run Your Application . . . . . . . . . . . . . . . . . . . . . . . 15

11 Congratulations! 15

12 Next Steps - Level Up Your Skills 15

Fun & Interactive Learning 1


Spring MVC Student Management Guide

What You’ll Build


Your Amazing Student Management System
Think of this as building a digital school where you can:

✓ Add new students - Like enrolling them in school

✓ View all students - Like checking the class roster

✓ Update student info - When they change their details

✓ Delete students - When they graduate or transfer

Your Coding Toolkit


Tool What It Does
Spring MVC Your web framework (the skeleton)
JDBC Your database connector (the bridge)
MySQL Your database (the filing cabinet)
Postman Your API tester (quality checker)
Eclipse Your coding playground
Maven Your project manager (organizer)

Phase 1: Setting Up Your Digital Workshop


Step 1: Create Your Maven Project in Eclipse
Welcome to Project Creation Circus!
Let’s create your first Spring MVC project step by step:

1. Open Eclipse IDE

2. Go to: File → New → Other → Maven → Maven Project

3. Check "Create a simple project"

4. Fill in the magical details:

• Group ID: com.studentapp


• Artifact ID: student-management
• Version: 1.0.0
• Packaging: war (we’re building a web app!)

Fun & Interactive Learning 2


Spring MVC Student Management Guide

Step 2: Configure Your pom.xml (The Recipe Book)


Your Shopping List of Dependencies
Add these ingredients to your pom.xml:

1 < properties >


2 < maven . compiler . source > 11 </ maven . compiler . source >
3 < maven . compiler . target > 11 </ maven . compiler . target >
4 < spring . version > 5.3.21 </ spring . version >
5 </ properties >
6

7 < dependencies >


8 <! -- Spring MVC ( Your Web Framework Hero ) -- >
9 < dependency >
10 < groupId > org . springframework </ groupId >
11 < artifactId > spring - webmvc </ artifactId >
12 < version > ${ spring . version } </ version >
13 </ dependency >
14

15 <! -- Spring JDBC ( Database Bridge Builder ) -- >


16 < dependency >
17 < groupId > org . springframework </ groupId >
18 < artifactId > spring - jdbc </ artifactId >
19 < version > ${ spring . version } </ version >
20 </ dependency >
21

22 <! -- MySQL Connector ( Database Whisperer ) -- >


23 < dependency >
24 < groupId > mysql </ groupId >
25 < artifactId > mysql - connector - java </ artifactId >
26 < version > 8.0.33 </ version >
27 </ dependency >
28

29 <! -- JSTL ( Web Page Magic ) -- >


30 < dependency >
31 < groupId > javax . servlet </ groupId >
32 < artifactId > jstl </ artifactId >
33 < version > 1.2 </ version >
34 </ dependency >
35

36 <! -- Servlet API ( Web Container Friend ) -- >


37 < dependency >
38 < groupId > javax . servlet </ groupId >
39 < artifactId > javax . servlet - api </ artifactId >
40 < version > 4.0.1 </ version >
41 < scope > provided </ scope >
42 </ dependency >
43 </ dependencies >

Fun & Interactive Learning 3


Spring MVC Student Management Guide

Phase 2: Building Your Project Architecture


Step 3: Create Your Folder Structure
Building Your Code House
Create these folders in src/main/java:

1. com.studentapp.controller - Your traffic controllers

2. com.studentapp.model - Your data blueprints

3. com.studentapp.dao - Your database workers

4. com.studentapp.config - Your configuration wizards

Step 4: Create the Student Model (Your Data Blueprint)


Student.java - The Heart of Our App

1 package com . studentapp . model ;


2

3 public class Student {


4 private int id ;
5 private String name ;
6 private String email ;
7 private String course ;
8 private int age ;
9

10 // Default constructor ( Empty house )


11 public Student () {}
12

13 // Parameterized constructor ( Furnished house )


14 public Student ( String name , String email , String course , int
age ) {
15 this . name = name ;
16 this . email = email ;
17 this . course = course ;
18 this . age = age ;
19 }
20

21 // Getters and Setters ( Your data access doors )


22 public int getId () { return id ; }
23 public void setId ( int id ) { this . id = id ; }
24

25 public String getName () { return name ; }


26 public void setName ( String name ) { this . name = name ; }
27

28 public String getEmail () { return email ; }

Fun & Interactive Learning 4


Spring MVC Student Management Guide

29 public void setEmail ( String email ) { this . email = email ; }


30

31 public String getCourse () { return course ; }


32 public void setCourse ( String course ) { this . course = course ; }
33

34 public int getAge () { return age ; }


35 public void setAge ( int age ) { this . age = age ; }
36

37 @Override
38 public String toString () {
39 return " Student { id = " + id + " , name = ’ " + name +
40 " ’, email = ’ " + email + " ’, course = ’ " + course +
41 " ’, age = " + age + " } " ;
42 }
43 }

Phase 3: Database Magic


Step 5: Create Your MySQL Database
Database Creation Theater
Open your MySQL Workbench and run these magical spells:

1 -- Create your student kingdom


2 CREATE DATABASE student_management ;
3

4 -- Enter your kingdom


5 USE student_management ;
6

7 -- Create the students table ( your data palace )


8 CREATE TABLE students (
9 id INT AUTO_INCREMENT PRIMARY KEY ,
10 name VARCHAR (100) NOT NULL ,
11 email VARCHAR (100) UNIQUE NOT NULL ,
12 course VARCHAR (50) NOT NULL ,
13 age INT NOT NULL
14 );
15

16 -- Add some sample students ( populate your kingdom )


17 INSERT INTO students ( name , email , course , age ) VALUES
18 ( ’ Alice Johnson ’ , ’ alice@email . com ’ , ’ Computer Science ’ , 20) ,
19 ( ’ Bob Smith ’ , ’ bob@email . com ’ , ’ Mathematics ’ , 22) ,
20 ( ’ Carol Brown ’ , ’ carol@email . com ’ , ’ Physics ’ , 21) ;

Fun & Interactive Learning 5


Spring MVC Student Management Guide

Step 6: Create the DAO Interface (Your Database Contract)


StudentDAO.java - Your Database Promise

1 package com . studentapp . dao ;


2

3 import java . util . List ;


4 import com . studentapp . model . Student ;
5

6 public interface StudentDAO {


7 // The four magical CRUD operations
8 void addStudent ( Student student ) ; // Create
9 List < Student > getAllStudents () ; // Read All
10 Student getStudentById ( int id ) ; // Read One
11 void updateStudent ( Student student ) ; // Update
12 void deleteStudent ( int id ) ; // Delete
13 }

Step 7: Implement the DAO (Your Database Worker)


StudentDAOImpl.java - The Database Hero

1 package com . studentapp . dao ;


2

3 import java . sql . ResultSet ;


4 import java . sql . SQLException ;
5 import java . util . List ;
6

7 import org . springframework . beans . factory . annotation . Autowired ;


8 import org . springframework . jdbc . core . JdbcTemplate ;
9 import org . springframework . jdbc . core . RowMapper ;
10 import org . springframework . stereotype . Repository ;
11

12 import com . studentapp . model . Student ;


13

14 @Repository
15 public class StudentDAOImpl implements StudentDAO {
16

17 @Autowired
18 private JdbcTemplate jdbcTemplate ;
19

20 // Student Row Mapper ( Data Translator )


21 private static class StudentRowMapper implements RowMapper <
Student > {
22 @Override
23 public Student mapRow ( ResultSet rs , int rowNum ) throws
SQLException {

Fun & Interactive Learning 6


Spring MVC Student Management Guide

24 Student student = new Student () ;


25 student . setId ( rs . getInt ( " id " ) ) ;
26 student . setName ( rs . getString ( " name " ) ) ;
27 student . setEmail ( rs . getString ( " email " ) ) ;
28 student . setCourse ( rs . getString ( " course " ) ) ;
29 student . setAge ( rs . getInt ( " age " ) ) ;
30 return student ;
31 }
32 }
33

34 @Override
35 public void addStudent ( Student student ) {
36 String sql = " INSERT INTO students ( name , email , course ,
age ) VALUES (? , ? , ? , ?) " ;
37 jdbcTemplate . update ( sql , student . getName () , student .
getEmail () ,
38 student . getCourse () , student . getAge () ) ;
39 }
40

41 @Override
42 public List < Student > getAllStudents () {
43 String sql = " SELECT * FROM students " ;
44 return jdbcTemplate . query ( sql , new StudentRowMapper () ) ;
45 }
46

47 @Override
48 public Student getStudentById ( int id ) {
49 String sql = " SELECT * FROM students WHERE id = ? " ;
50 return jdbcTemplate . queryForObject ( sql , new
StudentRowMapper () , id ) ;
51 }
52

53 @Override
54 public void updateStudent ( Student student ) {
55 String sql = " UPDATE students SET name =? , email =? , course
=? , age =? WHERE id =? " ;
56 jdbcTemplate . update ( sql , student . getName () , student .
getEmail () ,
57 student . getCourse () , student . getAge () ,
student . getId () ) ;
58 }
59

60 @Override
61 public void deleteStudent ( int id ) {
62 String sql = " DELETE FROM students WHERE id = ? " ;
63 jdbcTemplate . update ( sql , id ) ;
64 }
65 }

Fun & Interactive Learning 7


Spring MVC Student Management Guide

Phase 4: Creating Your Web Controllers


Step 8: Build the Student Controller (Your Traffic Director)
StudentController.java - The Command Center

1 package com . studentapp . controller ;


2

3 import java . util . List ;


4

5 import org . springframework . beans . factory . annotation . Autowired ;


6 import org . springframework . stereotype . Controller ;
7 import org . springframework . ui . Model ;
8 import org . springframework . web . bind . annotation .*;
9

10 import com . studentapp . dao . StudentDAO ;


11 import com . studentapp . model . Student ;
12

13 @Controller
14 @RequestMapping ( " / students " )
15 public class StudentController {
16

17 @Autowired
18 private StudentDAO studentDAO ;
19

20 // Home page - Show all students


21 @GetMapping ( " / " )
22 public String listStudents ( Model model ) {
23 List < Student > students = studentDAO . getAllStudents () ;
24 model . addAttribute ( " students " , students ) ;
25 return " student - list " ;
26 }
27

28 // Show form to add new student


29 @GetMapping ( " / add " )
30 public String showAddForm ( Model model ) {
31 model . addAttribute ( " student " , new Student () ) ;
32 return " student - form " ;
33 }
34

35 // Save new student


36 @PostMapping ( " / save " )
37 public String saveStudent ( @ModelAttribute ( " student " ) Student
student ) {
38 if ( student . getId () == 0) {
39 studentDAO . addStudent ( student ) ;
40 } else {
41 studentDAO . updateStudent ( student ) ;
42 }

Fun & Interactive Learning 8


Spring MVC Student Management Guide

43 return " redirect :/ students / " ;


44 }
45

46 // Show form to update student


47 @GetMapping ( " / edit /{ id } " )
48 public String showEditForm ( @PathVariable int id , Model model )
{
49 Student student = studentDAO . getStudentById ( id ) ;
50 model . addAttribute ( " student " , student ) ;
51 return " student - form " ;
52 }
53

54 // Delete student
55 @GetMapping ( " / delete /{ id } " )
56 public String deleteStudent ( @PathVariable int id ) {
57 studentDAO . deleteStudent ( id ) ;
58 return " redirect :/ students / " ;
59 }
60 }

Phase 5: Configuration Magic


Step 9: Create Database Configuration
DatabaseConfig.java - Your Database Wizard

1 package com . studentapp . config ;


2

3 import javax . sql . DataSource ;


4

5 import org . springframework . context . annotation . Bean ;


6 import org . springframework . context . annotation . Configuration ;
7 import org . springframework . jdbc . core . JdbcTemplate ;
8 import org . springframework . jdbc . datasource . Dr iverManage rDataSource
;
9

10 @Configuration
11 public class DatabaseConfig {
12

13 @Bean
14 public DataSource dataSource () {
15 Driver ManagerDat aSource dataSource = new
Driver ManagerData Source () ;
16 dataSource . setDriverClassName ( " com . mysql . cj . jdbc . Driver " ) ;
17 dataSource . setUrl ( " jdbc : mysql :// localhost :3306/
student_management " ) ;
18 dataSource . setUsername ( " your_username " ) ; // Change this !
19 dataSource . setPassword ( " your_password " ) ; // Change this !

Fun & Interactive Learning 9


Spring MVC Student Management Guide

20 return dataSource ;
21 }
22

23 @Bean
24 public JdbcTemplate jdbcTemplate () {
25 return new JdbcTemplate ( dataSource () ) ;
26 }
27 }

Step 10: Create Spring MVC Configuration


WebMvcConfig.java - Your MVC Conductor

1 package com . studentapp . config ;


2

3 import org . springframework . context . annotation . Bean ;


4 import org . springframework . context . annotation . ComponentScan ;
5 import org . springframework . context . annotation . Configuration ;
6 import org . springframework . web . servlet . ViewResolver ;
7 import org . springframework . web . servlet . config . annotation .
EnableWebMvc ;
8 import org . springframework . web . servlet . config . annotation .
Resour ceHandlerRe gistry ;
9 import org . springframework . web . servlet . config . annotation .
WebMvcConfigurer ;
10 import org . springframework . web . servlet . view .
Intern a l R es o u r c eV i e w R es o l v e r ;
11

12 @Configuration
13 @EnableWebMvc
14 @ComponentScan ( basePackages = " com . studentapp " )
15 public class WebMvcConfig implements WebMvcConfigurer {
16

17 @Bean
18 public ViewResolver viewResolver () {
19 I n t er n a l R es o u r c eV i e w R es o l v e r resolver = new
I n te r n a l R es o u r c eV i e w R es o l v e r () ;
20 resolver . setPrefix ( " / WEB - INF / views / " ) ;
21 resolver . setSuffix ( " . jsp " ) ;
22 return resolver ;
23 }
24

25 @Override
26 public void addResourceHandlers ( ResourceH andlerRegis try
registry ) {
27 registry . addResourceHandler ( " / resources /** " )
28 . addResourceLocations ( " / resources / " ) ;
29 }
30 }

Fun & Interactive Learning 10


Spring MVC Student Management Guide

Phase 6: Creating Beautiful Web Pages


Step 11: Create Student List Page
Create folder: src/main/webapp/WEB-INF/views/

student-list.jsp - Your Student Gallery

1 <% @ page language = " java " contentType = " text / html ; charset = UTF -8 " % >
2 <% @ taglib uri = " http :// java . sun . com / jsp / jstl / core " prefix = " c " % >
3 <! DOCTYPE html >
4 < html >
5 < head >
6 < title > Student Management System </ title >
7 < style >
8 body { font - family : Arial , sans - serif ; margin : 20 px ; }
9 table { width : 100%; border - collapse : collapse ; }
10 th , td { padding : 12 px ; text - align : left ; border : 1 px
solid # ddd ; }
11 th { background - color : #3498 db ; color : white ; }
12 . btn { padding : 8 px 16 px ; margin : 5 px ; text - decoration :
none ;
13 border - radius : 4 px ; color : white ; }
14 . btn - primary { background - color : #3498 db ; }
15 . btn - success { background - color : #27 ae60 ; }
16 . btn - danger { background - color : # e74c3c ; }
17 </ style >
18 </ head >
19 < body >
20 < h1 > Student Management System </ h1 >
21 <a href = " $ { pageContext . request . contextPath }/ students / add "
22 class = " btn btn - success " > Add New Student </ a >
23

24 < h2 > All Students </ h2 >


25 < table >
26 < thead >
27 < tr >
28 < th > ID </ th >
29 < th > Name </ th >
30 < th > Email </ th >
31 < th > Course </ th >
32 < th > Age </ th >
33 < th > Actions </ th >
34 </ tr >
35 </ thead >
36 < tbody >
37 <c : forEach var = " student " items = " $ { students } " >

Fun & Interactive Learning 11


Spring MVC Student Management Guide

38 < tr >
39 < td >$ { student . id } </ td >
40 < td >$ { student . name } </ td >
41 < td >$ { student . email } </ td >
42 < td >$ { student . course } </ td >
43 < td >$ { student . age } </ td >
44 < td >
45 <a href = " $ { pageContext . request . contextPath
}/ students / edit / $ { student . id } "
46 class = " btn btn - primary " > Edit </ a >
47 <a href = " $ { pageContext . request . contextPath
}/ students / delete / $ { student . id } "
48 class = " btn btn - danger "
49 onclick = " return confirm ( ’ Are you sure
? ’) " > Delete </ a >
50 </ td >
51 </ tr >
52 </ c : forEach >
53 </ tbody >
54 </ table >
55 </ body >
56 </ html >

Phase 7: Testing with Postman


Step 12: Create REST Controller for API Testing
StudentRestController.java - Your API Gateway

1 package com . studentapp . controller ;


2

3 import java . util . List ;


4

5 import org . springframework . beans . factory . annotation . Autowired ;


6 import org . springframework . web . bind . annotation .*;
7

8 import com . studentapp . dao . StudentDAO ;


9 import com . studentapp . model . Student ;
10

11 @RestController
12 @RequestMapping ( " / api / students " )
13 public class StudentRestController {
14

15 @Autowired
16 private StudentDAO studentDAO ;
17

18 @GetMapping
19 public List < Student > getAllStudents () {

Fun & Interactive Learning 12


Spring MVC Student Management Guide

20 return studentDAO . getAllStudents () ;


21 }
22

23 @GetMapping ( " /{ id } " )


24 public Student getStudent ( @PathVariable int id ) {
25 return studentDAO . getStudentById ( id ) ;
26 }
27

28 @PostMapping
29 public String addStudent ( @RequestBody Student student ) {
30 studentDAO . addStudent ( student ) ;
31 return " Student added successfully ! " ;
32 }
33

34 @PutMapping ( " /{ id } " )


35 public String updateStudent ( @PathVariable int id , @RequestBody
Student student ) {
36 student . setId ( id ) ;
37 studentDAO . updateStudent ( student ) ;
38 return " Student updated successfully ! " ;
39 }
40

41 @DeleteMapping ( " /{ id } " )


42 public String deleteStudent ( @PathVariable int id ) {
43 studentDAO . deleteStudent ( id ) ;
44 return " Student deleted successfully ! " ;
45 }
46 }

Step 13: Postman Testing Guide


Postman Testing Adventures
Test these endpoints in Postman:

1. GET All Students:

• URL: GET http://localhost:8080/student-management/api/students


• Expected: List of all students in JSON

2. GET Single Student:

• URL: GET http://localhost:8080/student-management/api/students/1


• Expected: Single student with ID 1

3. POST New Student:

• URL: POST http://localhost:8080/student-management/api/students


• Body (JSON):

Fun & Interactive Learning 13


Spring MVC Student Management Guide

1 {
2 " name ": " John Doe " ,
3 " email ": " john@email . com " ,
4 " course ": " Engineering " ,
5 " age ": 23
6 }

4. PUT Update Student:

• URL: PUT http://localhost:8080/student-management/api/students/1


• Body: Updated student JSON

5. DELETE Student:

• URL: DELETE http://localhost:8080/student-management/api/students/1


• Expected: Confirmation message

Phase 8: Launch Your Application


Step 14: Configure Web.xml
Create src/main/webapp/WEB-INF/web.xml:
1 <? xml version = " 1.0 " encoding = " UTF -8 " ? >
2 <web - app xmlns = " http: // xmlns . jcp . org / xml / ns / javaee "
3 xmlns:xsi = " http: // www . w3 . org /2001/ XMLSchema - instance "
4 xsi:schemaLocation = " http: // xmlns . jcp . org / xml / ns / javaee
5 http: // xmlns . jcp . org / xml / ns / javaee / web - app_4_0 . xsd "
6 version = " 4.0 " >
7

8 < display - name > Student Management System </ display - name >
9

10 < servlet >


11 < servlet - name > springDispatcher </ servlet - name >
12 < servlet - class > org . springframework . web . servlet .
DispatcherServlet </ servlet - class >
13 < init - param >
14 < param - name > contextClass </ param - name >
15 < param - value > org . springframework . web . context . support .
A n n o t a t i o n C o n f i g W e b A p p l i c a t i o n C o n t e x t </ param - value >
16 </ init - param >
17 < init - param >
18 < param - name > contextConfigLocation </ param - name >
19 < param - value > com . studentapp . config </ param - value >
20 </ init - param >
21 < load - on - startup >1 </ load - on - startup >
22 </ servlet >
23

24 < servlet - mapping >


25 < servlet - name > springDispatcher </ servlet - name >

Fun & Interactive Learning 14


Spring MVC Student Management Guide

26 <url - pattern >/ </ url - pattern >


27 </ servlet - mapping >
28 </ web - app >

Step 15: Run Your Application


Launch Sequence
1. Right-click project → Run As → Run on Server

2. Choose Apache Tomcat (or your preferred server)

3. Access your app at: http://localhost:8080/student-management/students/

4. Celebrate your success!

Congratulations!

YOU DID IT!


You’ve successfully built a complete Student Management
System with:
• Spring MVC for web framework
• JDBC for database connectivity
• MySQL for data storage
• RESTful APIs for Postman testing
• Beautiful web interface
You’re now a Spring MVC wizard!

Next Steps - Level Up Your Skills


1. Add input validation and error handling

2. Implement pagination for large datasets

3. Add search and filter functionality

4. Create unit tests with JUnit

5. Add Spring Security for authentication

6. Deploy to cloud platforms (AWS, Heroku)

Fun & Interactive Learning 15


Spring MVC Student Management Guide

Happy Coding! Keep building amazing things!

Fun & Interactive Learning 16

You might also like