VOOZH about

URL: https://dzone.com/articles/spring-boot-2-with-jsp-view

⇱ Spring Boot 2 With JSP View


Related

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot 2 With JSP View

Spring Boot 2 With JSP View

Creating a Spring Boot 2 web app just got a lot easier.

By Updated Dec. 29, 20 · Tutorial
Likes
Comment
Save
113.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, I am going to show you how easy it is to create a web application with Spring Boot 2, along with the embedded Tomcat + JSP template and JSP views.

What You'll Need

1. Project Structure

You can get the blueprint from the Spring Initializer page.

2. Project Dependencies

XML




x
55


1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
   <modelVersion>4.0.0</modelVersion>
5
   <parent>
6
      <groupId>org.springframework.boot</groupId>
7
      <artifactId>spring-boot-starter-parent</artifactId>
8
      <version>2.1.6.RELEASE</version>
9
      <relativePath/> <!-- lookup parent from repository -->
10
   </parent>
11
   <groupId>com.eprogrammerz.examples.spring</groupId>
12
   <artifactId>spring-boot-jsp</artifactId>
13
   <version>0.0.1-SNAPSHOT</version>
14
   <name>spring-boot-jsp</name>
15
   <description>Example Spring Boot with JSP view</description>
16

 
17
   <properties>
18
      <java.version>1.8</java.version>
19
   </properties>
20

 
21
   <dependencies>
22
      <dependency>
23
         <groupId>org.springframework.boot</groupId>
24
         <artifactId>spring-boot-starter-web</artifactId>
25
      </dependency>
26

 
27
      <dependency>
28
         <groupId>org.springframework.boot</groupId>
29
         <artifactId>spring-boot-starter-tomcat</artifactId>
30
         <scope>provided</scope>
31
      </dependency>
32

 
33
      <dependency>
34
         <groupId>org.apache.tomcat.embed</groupId>
35
         <artifactId>tomcat-embed-jasper</artifactId>
36
         <scope>provided</scope>
37
      </dependency>
38

 
39
      <dependency>
40
         <groupId>org.springframework.boot</groupId>
41
         <artifactId>spring-boot-starter-test</artifactId>
42
         <scope>test</scope>
43
      </dependency>
44
   </dependencies>
45

 
46
   <build>
47
      <plugins>
48
         <plugin>
49
            <groupId>org.springframework.boot</groupId>
50
            <artifactId>spring-boot-maven-plugin</artifactId>
51
         </plugin>
52
      </plugins>
53
   </build>
54

 
55
</project>


3. Configuration

3.1 Application Configurations

This SpringBootServletInitializer runs a SpringBootJspApplication from a traditional WAR deployment

Java




xxxxxxxxxx
1
19


1
package com.eprogrammerz.examples.spring.springbootjsp;
2

 
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.builder.SpringApplicationBuilder;
6
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7

 
8
@SpringBootApplication
9
public class SpringBootJspApplication extends SpringBootServletInitializer {
10
   @Override
11
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
12
      return application.sources(SpringBootJspApplication.class);
13
   }
14

 
15
   public static void main(String[] args) {
16
      SpringApplication.run(SpringBootJspApplication.class, args);
17
   }
18

 
19
}


3.2 Resources

Properties files




xxxxxxxxxx
1


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


4. Controller and View Template

4.1 Create a Controller With Simple Method Handling the Base Request

Java




xxxxxxxxxx
1
16


1
package com.eprogrammerz.examples.spring.springbootjsp.controllers;
2

 
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.web.bind.annotation.GetMapping;
6
import org.springframework.web.bind.annotation.RequestParam;
7

 
8
@Controller
9
public class HelloController {
10
    @GetMapping({"/", "/hello"})
11
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
12
        model.addAttribute("name", name);
13
        return "hello";
14
   }
15
}
16

 


4.2 Add JSP View as a Template

For JSP files, put in src/main/webapp/WEB-INF/jsp/

HTML




xxxxxxxxxx
1
10


1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="UTF-8">
5
    <title>Hello ${name}!</title>
6
</head>
7
<body>
8
    <h2 class="hello-title">Hello ${name}!</h2>
9
</body>
10
</html>


5. Run With Maven

You can run your application with a bash command at the project root directory:

Shell




xxxxxxxxxx
1


1
mvn clean spring-boot:run


Then go to localhost:8080 to test out your application.

That's it!

All code is available on GitHub.

Spring Framework Spring Boot

Published at DZone with permission of Yogen Rai. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: