Spring MVC architecture is a part of the Spring Framework that allows building flexible and loosely coupled web applications. One of the most commonly used view technologies with Spring MVC is JSP (JavaServer Pages). This article covers how JSP integrates with Spring MVC
What is Spring MVC?
Spring MVC is a web framework based on the Model-View-Controller design pattern. The terms model, view and controller are defined as follows:
- Model: The Model encapsulates the application data.
- View: The View renders the model data and generates HTML output that the client's browser can interpret.
- Controller: The Controller processes the user requests and passes them to the view for rendering.
Step to implement Spring MVC with JSP
Step 1: Create the Project
- Eclipse IDE: File -> New -> Dynamic Web Project (check "Generate web.xml")
- IntelliJ: New -> Project > Maven -> Web App archetype
Step 2: Add Maven Dependencies
Add below dependecies in pom.xml file:
Java
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
This file tells the servlet container how to load the Spring DispatcherServlet and which file has Spring configurations.
web.xml
Java
<web-app>
<display-name>SampleMVC</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
/index.html
</welcome-file>
</welcome-file-list>
Step 4: Create Spring Configuration file
This file initializes Spring MVC, scans your Java classes, and sets up view resolution (JSP mapping) WebApplicationContext. And this contains the MVC-specific configurations including view-resolvers, datasource, messagesource, multipart-resolver (file-upload), etc.
mvc-dispatcher-servlet.xml
Java
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="...">
<!-- Scan all Java classes in this package for Spring annotations -->
<context:component-scan base-package="com.springsamples" />
<!-- Enable Spring MVC Annotations (@Controller, @GetMapping, etc.) -->
<mvc:annotation-driven />
<!-- Allow static content (CSS, JS, images) -->
<mvc:default-servlet-handler />
<!-- Configure JSP view resolver -->
<bean id="viewProvider" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Step 5: Create the Controller
Java
package com.springsamples.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@GetMapping("/") public ModelAndView firstView()
{
// "greet" will map to greet.jsp
ModelAndView mav = new ModelAndView("greet");
mav.addObject(
"greeting",
"GeeksForGeeks Welcomes you to Spring!");
return mav;
}
}
Step 6: Create the JSP View.
Place this file inside: src/main/webapp/greet.jsp
greet.jsp
Java
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-// W3C// DTD HTML 4.01
Transitional// EN" "http:// www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Start Spring MVC</title>
</head>
<body>
<h1>Start here</h1>
${greeting}
</body>
</html>
When you run the project and open the browser at:
http://localhost:8080/SpringMVCApp/
Output:
Output
Similar Reads
ViewResolver in Spring MVC Spring MVC is a powerful Web MVC Framework for building web applications. It provides a structured way to develop web applications by separating concerns into Model, View, and Controller. One of the key features of Spring MVC is the ViewResolver, which enables you to render models in the browser wit
7 min read
Spring MVC Matrix Variables In this article, we will learn about Spring MVC Matrix Variables. Matrix Variable is used to parse and bind name-value pairs inside a path segment to method parameters. A semicolon separates many pairs. Matrix variables must first be enabled. XML Matrix VariableBelow are the necessary dependencies a
3 min read
Top 10 Spring MVC Annotations with Examples In the world of Spring MVC, annotations are like a reliable assistant for your web applications. They usually work behind the scenes of your application and make tasks easier. Also, they help the developers to communicate clearly with their applications and ensure what the developers actually want t
10 min read
Spring MVC - WebMvcConfigure Spring WebMvcConfigurer enables developers to customize the default MVC setups in Spring applications. By implementing this interface, users can customize several features of the MVC framework, such as interceptors, resource handling, and view controllers. In this article, we will learn how to Custo
4 min read
How to Create Your First View in Spring MVC? Spring MVC is a powerful Web MVC Framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
5 min read
Spring - MVC Framework The Spring MVC Framework follows the Model-View-Controller architectural design pattern, which works around the Front Controller, i.e., the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapp
4 min read