1. Overview

Here is Spring boot JSP example using maven. Spring boot provides easy integration with spring MVC with Model-View-Controller application. There is some limitation of JSP while working with Spring boot. You can read those JSP limitations from here.

Technology:

  1. Spring Boot
  2. Java 8
  3. JSP
  4. Maven

 2. Example

 

Spring boot jsp example project structure

Spring boot jsp example project structure

2.1 pom.xml

While working with spring boot with JSP there is some extra dependency will require that configuration as under:

<?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>

    <groupId>spring-boot-example</groupId>
    <artifactId>Spring-boot-jsp-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <description>spring boot jsp example</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- Provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>                <!-- for tomcat web container-->
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>              <!--fot jap compilation need provide scope runtime or provided because it available in tomcat -->
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>runtime</scope>     <!-- in my case provided not working so write runtime-->
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>           <!-- for jsp jstl tags-->
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>         <!--its for spring mvc related -->
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.1 SpringBootConfig

Base file from where Spring boot application will start.

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan
// Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootConfig.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it will start application
    }
}

application.properties

application.properties contains configuration related to view location. When controller return view name based on spring.mvc.view.prefix and spring.mvc.view.suffix properties spring boot will find actual view name.

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

SpringBootJSPExampleController

Here is Spring boot controller will accept require comes from clients and send jsp names.

package com.javadeveloperzone.controller;

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

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@Controller
public class SpringBootJSPExampleController {

    @GetMapping("/welcome")                     // it will handle all request for /welcome
    public String SpringBootHello() {
        return "welcome";           // welcome is view name. It will call welcome.jsp
    }
}

welcome.jsp

jsp page also support JSTL tags.

<html>
<head>
    <title>Spring boot jsp example</title>
</head>
<body>
<h2>Spring boot jsp example</h2>
</body>
</html>

Demo:

When we build using mvn build at that time it will generate .war file that we can deploy in any JEE web server like tomcat.

Let’s open our favorite browser and open URL  localhost:8080/welcome

 

Spring boot jsp example output

Spring boot JSP example output

 

3. References

4. Source Code

spring-boot-jsp-example (36 KB)

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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