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:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <groupId>com.surasint.example</groupId>
    <artifactId>spring-boot-02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- jstl for jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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

This line:

<packaging>war</packaging>

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:

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

They are for JSP in Spring Boot.

These lines:

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

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).

package com.surasint.example;

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

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {

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

}

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

package com.surasint.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;
import java.util.Map;

@Controller
public class TestController {

	@GetMapping("/testjsp")
	public String list(Map<String, Object> model) {
		model.put("this_time",new Date().toString());
		return "testjsp-view";
	}
}

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

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

Next, create testjsp-view.jsp:

<!doctype html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>
</head>

<body>
    <div><%="hello for jsp :"%> ${this_time}</div>
</body>

</html>

Try:

run this in console:

mvn clean package

Then run this to start:

mvn clean spring-boot:run

You should see something like this:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

.....
.....
2017-12-12 22:03:04.243  INFO 260 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotation
ConfigEmbeddedWebApplicationContext@7595f8d7: startup date [Tue Dec 12 22:03:01 CET 2017]; root of context hierarchy

Open a browser and try to go to this url:

http://localhost:8080/testjsp

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:

mvn clean package

Then you should have this jar in your the “target” folder. Like this:

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

java -jar spring-boot-02-1.0-SNAPSHOT.war

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