Spring Boot JSP Hello World Tutorial with Example

Spring Boot JSP example – Spring Boot has gained a lot of traction in past few years. Thanks to its opinionated approach on Java web application, its very easy to start a Web project and get up and running in no time. In this tutorial we will create a simple Spring Boot based application which will render view using JSP. Most of the code can be auto-generated using start.spring.io.

We will use following technologies for this project.

  1. Spring Boot v1.5.7
  2. Java 8
  3. Maven 3.3.9

1. Maven Dependencies for Spring Boot JSP

Let’s look at the directory structure first. This is usual maven based web project with src/ test/ and resources/ folder. Note that the main class for our Spring Boot JSP example project is SpringBootJspExampleApplication.

spring boot jsp example project structure

Following is the pom.xml that defines the dependencies for our hello world Spring Boot JSP project.

pom.xml

Spring Boot takes care of all the dependencies version through starter pom. We have defined the parent pom as spring-boot-starter-parent. Parent pom defines all the versioning so that we do not have to worry about version compatibility of different libraries. We have defined compiled time dependency spring-boot-starter-web and jstl for our JSP example. This will include all required Spring Web MVC and other dependencies.

2. Spring Boot Application Class

Once the Maven pom is in place, let’s check the Main class SpringBootJspExampleApplication.

SpringBootJspExampleApplication.java
package net.viralpatel.springbootjspexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootJspExampleApplication  extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootJspExampleApplication.class);
	}

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SpringBootJspExampleApplication.class, args);
	}

}

As we saw earlier, this class is the starting point in Spring Boot application. We have annotated it with @SpringBootApplication annotation. Spring Boot provides this convenient annotation which is simply wraps @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.

@Configuration annotation marks this class as a configuration class. At runtime, spring will instantiate bean of this class and register it with Application context. @Configuration also indicates that this class can have one or more @Bean methods which Spring will use to register custom bean definitions.

@EnableAutoConfiguration is the magic annotation that Spring Boot uses to do its magic trick. This annotation enables Spring’s auto-configuration mechanism to load Spring’s application context with some default beans and configuration.

@ComponentScan annotation defines the base package from where Spring will start scanning for beans with stereotype annotations (@Controller, @Service, @Component etc.) Since we have simply defined @SpringBootApplication annotation, Spring will only scan package where SpringBootJspExampleApplication is defined (net.viralpatel.springbootjspexample) and all children packages (net.viralpatel.springbootjspexample.*). If some of your beans are not initialised in your Spring Boot application, then its always good to first see where your SpringBootApplication class sits.

Our SpringBootJspExampleApplication class extends SpringBootServletInitializer class. This is Spring boot’s class is an opinionated implementation of WebApplicationInitializer which provides Java based (programmatic) configuration for ServletContext. This class makes it possible to define web app without web.xml.

3. Application properties for View resolver

Since this Spring Boot hello world app uses JSP to render view, lets define following properties in application.properties file.

application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

application.message: Hello World!!

Spring uses ViewResolver to translate the view names in @Controller to actual View. The Spring auto-configuration (in this case WebMvcAutoConfiguration) will add few default ViewResolvers in your context. One of such view resolver is InternalResourceViewResolver. This defines prefix and suffix to the view name. We can override prefix and suffix by defining spring.mvc.view.prefix and spring.mvc.view.suffix in application.properties.

Now let’s check the Welcome Controller of our Spring Boot JSP example project.

WelcomeController.java
package net.viralpatel.springbootjspexample;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class WelcomeController {

	@Value("${application.message:Hello World}")
	private String helloMessage;
	
	@GetMapping("/")
	public String welcome(Map model) {
		
		model.put("message", helloMessage);
		
		return "welcome";
	}
}

Nothing fancy in our WelcomeController. It defines welcome() method which maps “/” endpoint using @GetMapping (request mapping) annotation. The model is an hashmap that Spring will inject where we can put our data for view. Note that @Value annotation injects the property application.message in our controller. We can define default value for @Value attributes after : colon. Our welcome() method returns the view name “welcome” which Spring will map to JSP view (using InternalResourceViewResolver).

4. Static files – style.css

We can add any static content such as javascripts, css in /static directory. Create style.css under /static and copy following content.

style.css
body {
	font-size: 2em;
}

5. View – Welcome.jsp

And finally lets define the welcome.jsp under /WEB-INF/jsp/ directory.

welcome.jsp

We have referenced our stylesheet style.css in this JSP.

That’s all folks

Run the Spring Boot JSP example project by running SpringBootJspExampleApplication class as Java class or using maven mvn spring-boot:run command.

Once the application starts, launch the browser and open:
http://localhost:8080/

spring boot jsp example hello world demo

Download Source Code – Spring Boot JSP example

Source code of this Spring Boot JSP MVC Hello World tutorial is available in Github.

Github – spring-boot-jsp-example

Get our Articles via Email. Enter your email address.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *