Novel Coronavirus 🦠 is still holding back the restart of the world, but we can’t stop learning. Here we show you a little bit of knowledge that is not commonly used in development now, hoping to inspire you. In Spring Boot 2, you can use JSP view to create a Spring Boot 2 application. In Spring Boot 2, you can use JSP view.

Let’s see what we need

The project structure

You can get the project framework from Spring Initializer.

Project depend on

pom.xml


      
<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 - the 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>
Copy the code

configuration

  1. Startup Class Configuration

SpringBootServletInitializer according to the traditional way of WAR file to deploy to run SpringBootJspApplication. SpringBootJspApplication.java

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); }}Copy the code
  1. Resources

application.properties

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

Controller and View Template

  1. Write a Controller for a simple mapping method
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"; }}Copy the code
  1. Save the following contents as a JSP file and place it insrc/main/webapp/WEB-INF/jsp/directory
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${name}!</title>
</head>
<body>
    <h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>
Copy the code

Running with Maven

Run the program from the command line in the project root path.

mvn clean spring-boot:run
Copy the code

Visit localhost:8080 to test your program.

At this point, using Spring Boot 2 show JSP page basic configuration is complete, I hope to have some help to you.

In advance ㊗️ everyone New Year new weather, 2021 technology on a new step!

The author of this article: Hao Zitao WorldBlog. Chuangzhi8. Cn/posts / 11 – sp…Copyright Notice: This article is made byHow about window paper? – There’s plenty of itCreative, licensed under CC BY-NC-SA 3.0. It can be reproduced and quoted freely, but the author must be signed and indicate the source of the article. If reprinted to wechat official account, please add the following author’s official account qr code at the end of the article