📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Before you get started, check out Spring Data JPA Tutorial and Spring MVC Tutorial
Tools and technologies used
- Spring MVC - 5.1.0 RELEASE
- JDK - 1.8 or later
- Maven - 3.5.1
- Apache Tomcat - 8.5
- IDE - STS/Eclipse Neon.3
- JSTL - 1.2.1
Spring MVC Hello World Application Flow
Development Steps
- Create Maven Web Application
- Add Dependencies - pom.xml File
- Project Structure
- Spring Configuration - AppConfig.java
- Servlet Container Initialization - MySpringMvcDispatcherServletInitializer.java
- Model Class - HelloWorld.java
- Controller Class - HelloWorldController.java
- View - helloworld.jsp
- Build + Deploy + Run an application
- Demo
1. Create Maven Web Application
2. Add Dependencies - pom.xml File
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.springmvc</groupId>
<artifactId>springmvc5-helloworld-exmaple</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc5-helloworld-exmaple Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. Project Structure
4. Spring Configuration - AppConfig.java
package net.javaguides.springmvc.helloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* @author Ramesh Fadatare
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"net.javaguides.springmvc.helloworld"
})
public class AppConfig {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
- The @Configuration is a class-level annotation indicating that an object is a source of bean definitions.
- The @EnableWebMvc enables default Spring MVC configuration and provides the functionality equivalent to mvc:annotation-driven/ element in XML based configuration.
- The @ComponentScan scans the stereotype annotations (@Controller, @Service, etc...) in a package specified by basePackages attribute.
InternalResourceViewResolver
@Bean public InternalResourceViewResolver resolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
5. Servlet Container Initialization - SpringMvcDispatcherServletInitializer.java
package net.javaguides.springmvc.helloworld.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @author Ramesh Fadatare
*/
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class <?> [] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class <?> [] getServletConfigClasses() {
return new Class[] {
AppConfig.class
};
}
@Override
protected String[] getServletMappings() {
return new String[] {
"/"
};
}
}
6. Model Class - HelloWorld.java
package net.javaguides.springmvc.helloworld.model;
public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
7. Controller Class - HelloWorldController.java
package net.javaguides.springmvc.helloworld.controller;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import net.javaguides.springmvc.helloworld.model.HelloWorld;
/**
* @author Ramesh Fadatare
*/
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld")
public String handler(Model model) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Hello World Example Using Spring MVC 5!!!");
helloWorld.setDateTime(LocalDateTime.now().toString());
model.addAttribute("helloWorld", helloWorld);
return "helloworld";
}
}
8. View - helloworld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head><%@ page isELIgnored="false" %>
<meta charset="ISO-8859-1">
<title>Spring 5 MVC - Hello World Example | javaguides.net</title>
</head>
<body>
<h2>${helloWorld.message}</h2>
<h4>Server date time is : ${helloWorld.dateTime}</h4>
</body>
</html>
9. Build + Deploy + Run an application
clean install
10. Demo
Source Code on GitHub
The source code of this tutorial available on my GitHub repository at spring-mvc-tutorials.
the page shows:
ReplyDelete${helloWorld.message}
Server date time is : ${helloWorld.dateTime}
Can you check all JSTL jar files and configuration added?. Clone source code of this tutorial from my GitHub repository Spring MVC Tutorial - GitHub Repository
DeleteI get the same error, and I really don't want to clone your repository so how can we check where the jstl jar files and configurations are added
DeleteCheck in my article, pom.xml has two JSTL Dependency and <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> code in JSP file. This should work.
DeleteHello Ramesh,
DeleteThis seems like a good article. But I am facing exact same issue. Both jstl-api and jstl dependencies exists in the pom file and in .jsp file mentioned uri exists, but I am also getting $ related issue. not being processed
Found the issue.
DeleteTake a look at Mkyong's page
https://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/
Correct. I haven't used old JSP 1.2 descriptor so it worked for me. If you are using old JSP 1.2 descriptor then this link will solve your issue - https://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/
DeleteAdd <%@ page isELIgnored="false" %> line within a head of helloworld.jsp file should resolve your issue. I added this line so try now and let me know if you face any issues.
DeleteI get the same error, and I really don't know how do this, please help
Deletehi I have a 404 resource not available error and this tomcat message: "org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET"
ReplyDeleteAny idea why?
thanks
Hello Ramesh,
ReplyDeleteIn the above tutorial we have defined our JSp file inside WEB-INF folder, How could we directly hit the page from url? Definitely getting the 404
Hi again, If we put the helloWorld.jsp data inside the index.jsp our model is not getting the data on the front page, the controller is not being hit at all. could you please help.
ReplyDeleteThis SIMPLY DON't work.
ReplyDeleteIt worked perfectly for me.
ReplyDeleteThank u very much. It worked perfectly
ReplyDeletehow the index.jsp page invoked?
ReplyDelete