SpringBoot: How to enable JSP in IntelliJ

Hi there!

Are you having troubles configuring SpringBoot to cooperate well with JSPs in IntelliJ? Maybe you are having Whitelabel error?
I have found soultion to this problem. Here’s short tutorial how to make things work.

Step 0: Create spring boot project with spring initializr.

If you havent done this yet, create new project with spring initializr. If you are using IntelliJ community edition, you can create new project here:
http://start.spring.io/
In this tutorial, I have chosen REST and WEB dependencies; all the other properties were taken as default.

Step 1: Create index.jsp

First of all, you need to create index.jsp. Here is my project’s tree:

  1. ├── src
  2. │   ├── main
  3. │   │   ├── java
  4. │   │   │   └── com
  5. │   │   │       └── example
  6. │   │   │           ├── contoller
  7. │   │   │           │   ├── MainController.java
  8. │   │   │           │   └── SampleRestContoller.java
  9. │   │   │           └── DemoApplication.java
  10. │   │   ├── resources
  11. │   │   │   ├── application.properties
  12. │   │   │   ├── static
  13. │   │   │   └── templates
  14. │   │   └── webapp
  15. │   │       └── WEB-INF
  16. │   │           └── jsp
  17. │   │               └── index.jsp

If you want to work with JSPs, you need to create new directories under main:

  1. │   │   └── webapp
  2. │   │       └── WEB-INF
  3. │   │           └── jsp
  4. │   │               └── index.jsp

Step 2: Create application.properties file

You should create application.properties file inside resources directory:
Zrzut ekranu z 2017-02-22 19:40:43.png

Open this file, paste following code and save.

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

If you are interested in setting some other properties, you can find them in spring boot docs:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Step 3: Adding dependencies in pom.xml

Now you should add following dependencies to your pom.xml file.

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

IMPORTANT: I have seen a lot of tutorials on how to make JSPs work in spring boot. Most of them were created for STS IDE. In IntelliJ you CAN NOT put provided under tomcat-embed-jasper!

Step 4: Adding proper controller

Here is how to make proper controller for JSP. Note that im using @Controller annotation instead of @RestController.

@Controller
public class MainController {

    @GetMapping("/")
    public String home(){
        return "index";
    }
}

Step 5: 

Run the project and have fun 🙂

That’s it for today. If you are having troubles with setting things up, here you can find working source code on my github:

https://github.com/arturk9/springbootJSP

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s