7

Spring Boot 2 With JSP View

 3 years ago
source link: https://dzone.com/articles/spring-boot-2-with-jsp-view
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring Boot 2 With JSP View

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

Dec. 29, 20 · Java Zone · Tutorial

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

pom.xml

<?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>2.1.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.eprogrammerz.examples.spring</groupId>
   <artifactId>spring-boot-jsp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>spring-boot-jsp</name>
   <description>Example Spring Boot with JSP view</description>
   <properties>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

3. Configuration

3.1 Application Configurations

This SpringBootServletInitializer runs a SpringBootJspApplication from a traditional WAR deployment

SpringBootJspApplication.java

xxxxxxxxxx
package com.eprogrammerz.examples.spring.springbootjsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootJspApplication extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(SpringBootJspApplication.class);
   }
   public static void main(String[] args) {
      SpringApplication.run(SpringBootJspApplication.class, args);
   }
}

3.2 Resources

application.properties

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

4. Controller and View Template

4.1 Create a Controller With Simple Method Handling the Base Request

xxxxxxxxxx
package com.eprogrammerz.examples.spring.springbootjsp.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
    @GetMapping({"/", "/hello"})
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}

4.2 Add JSP View as a Template

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

xxxxxxxxxx
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${name}!</title>
</head>
<body>
    <h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>

5. Run With Maven

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

Shell
xxxxxxxxxx
mvn clean spring-boot:run

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

That's it!

All code is available on GitHub.

Topics:
java, spring boot 2, spring boot, jsp view, jsp, tutorial, spring boot tutorial

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

Opinions expressed by DZone contributors are their own.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK