Spring Boot with JSP – Example

You just want to see an minimal example of Spring boot with JSP and JSTL.

This page is for you.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 1.5.8.RELEASE:

2_spring-boot-jsp.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Call an url  then the service will call a JSP file which has JSTL tag

Explain:

This is the file structure:

In pom.xml you need this:

The lines which are highlighted are for JSP and JSTL. The other lines are general configuration for Spring Boot.

This line:

It is for build as an executable war file. Normally, you will see some examples on internet using <packaging>jar</packaging>, when you create an executable jar file, which it will does not work, you will get 404 and say that you cannot find JSP files.

These lines:

They are for JSP in Spring Boot.

These lines:

They are for JSTL in JSP. You can remove it if you do not want to use.

Then you create a SpringBootApplication class, for example MyApplication (Nothing special it is just for Spring Boot).

Next, create an endpoint when you access http://localhost:8080/testjsp

Next, create application.properties to tell Spring boot where you keep jsp files:

Next, create testjsp-view.jsp:

Try:

run this in console:

Then run this to start:

You should see something like this:

Open a browser and try to go to this url:

Then you should see this:

You can stop the process by just Ctrl+C (in Windows)

You can create a executable war file by run:

Then you should have this jar in your the “target” folder. For example,

You can also run this command to start the spring boot from the jar file.

Try to access to http://localhost:8080/testjsp. It will give you the same result.

Note: if you need to deploy the war file in Tomcat, you have to follow this Spring Boot – create War for Tomcat