CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   JAVA   How to Enable JSP Support in Spring Boot With HelloWorld Exa ...

How to Enable JSP Support in Spring Boot With HelloWorld Example Web Application ?

March 18, 2018 by SNK

enable jsp support in spring boot application with hello world example

Today in this tutorial we are going to see how can we enable jsp support in spring boot. As we know that spring boot is really impressive framework which was mainly focused to develop restful robust applications.

It is also being used to develop large enterprise level application combining with advance features of web services but many of the users are using its features to small scale applications also and for that most of the users are using jsp.

JSP is not enabled by default in spring boot so we need to tell the view-resolver of spring boot to add JSP support to our applications. We will follow step by step process with an HelloWorld Example Application.

Recently i have written tutorial on how to setup spring boot application using intellij and eclipse. Please follow that tutorial to setup fresh spring boot application and follow this article afterwards.

If you have finished setting up fresh spring boot application follow the guide below.

Lets get started

I assume that you have clean installation of spring boot application ready.

To enable jsp support in spring boot, we need to tell the view-resolver to use jsp. We are going to do that by adding some lines inside application.properties files inside the resources folder.

Paste these lines inside application.properties

application.properties
C
1
2
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

Adding these lines of codes will automatically enable JSP support in your application. Wait its not finished yet.

As we know that spring boot comes with embedded tomcat and it wont run until you will also say tomcat to have support for JSP. So add tomcat jasper dependency in your pom.xml just after the dependency of spring-boot-starter-web.

pom.xml
XHTML
1
2
3
4
5
<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
</dependency>

Note :- Eclipse will automatically detect and run yourapplication but if you are using it through intellij IDEA, application will not run. To solve that issue just change the tomcat embedded jasper scope from provided to default.

pom.xml
XHTML
1
2
3
4
5
<dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>default</scope>
</dependency>

Lets now test and run application with our helloworld example.

Create new class called helloworld.java or you can give any name you like inside src/main/java directory.

Annotate it with @Controller instead of @RestController because it will always try to send back json data instead of JSP will not be returned. Take a look at the code below.

HelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.codeinhouse.springboot;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HelloWorld {
    
    
    @RequestMapping("/")
    public String getHelloWorld() {
        return "hello";
    }
}

We have said our view-resolver to look for JSP inside WEB-INF directory in our application.properties file. Files out side of web-inf directory will not be scanned for jsp.

Create new JSP page inside WEB-INF and paste the code below.

hello.jsp
1
2
3
4
5
6
7
8
<html>
  <head>
    <title>Simple Hello World Exmaple - Spring Boot Application</title>
  </head>
  <body>
    <h1>Hello World By Codeinhouse</h1>
  </body>
</html>

This is it. Test and run your application to see the results. If you encounter any errors feel free to discuss them below.

SHARE ON
Buffer

Enjoyed this article?

Like us on

JAVA spring boot hello world example spring boot hello world example in eclipse spring boot jsp example spring boot jsp view resolver

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.