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
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
.
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
.
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.
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.
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.
Leave a Reply