Problem-solving ideas for integrating JSP with Spring Boot

Although Spring Boot no longer supports JSP by default, an integration operation has been carried out based on the principle of learning first in the case of multiple data searches.

1 Maven Dependency

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- JSP rendering engine -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <!-- If it is external tomcat If deployed, provided Property needs to be turned on -->
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <!-- JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2 Controller Code

@Controller
public class JspOnSpringBootController {

    // Read the configuration from application.yml, if Hello Jsp is not the default value
    @Value("${application.hello:Hello Jsp}")
    private String hello = "Hello Jsp";

    /**
     * Default page <br/>
     * @RequestMapping("/") Is different from @RequestMapping
     * If you do not write parameters, it is the global default page, which is also automatically accessed when you add an input 404 page.
     * If the parameter'/'is added, it is considered the root page only.
     * This method can be accessed through localhost:7070 or localhost:7070/index
     */
    @RequestMapping(value = {"/","/index"})
    public String index(Model model) {
        // Returns the string directly, and by default the frame goes to the (index spliced spring.view.suffix) page in the spring.view.prefix directory
        // This example is/WEB-INF/jsp/index.jsp
        model.addAttribute("name", "Landy");
        model.addAttribute("hello", hello);

        return "index";
    }

}

3 application.properties configuration

#service Port (Dev)
server.port=7070

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

application.hello = hello jsp on spring boot

4 Jsp Page Code

<html>
<head>
    <title>JSP on Spring Boot</title>
</head>
<body>
    <h1 style="color: red">${name}, ${hello}</h1>

</body>
</html>

5 Run

By all means, you can run and see the results of the page, but if the main method of running SpringApplication directly does not see the results, report 404.

See article to find out the root cause after searching on the Internet Why must you start by spring-boot:run after integrating jsp See the specific reason.

All three modes work well.

5.1 spring-boot:run start

Run with the command maven clean spring-boot:run or the maven plug-in for idea.

Access address: http://localhost:7070/index

5.2 bundled into war s

  1. Setting the pom.xml file packaging property should be war.

    <groupId>org.landy</groupId>
    <artifactId>spring-boot-lesson-4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
  2. Execute the packaging command maven clean package
  3. Execute run war package command java-jar war package name

  1. Access address: http://localhost:7070/index

5.3 External tomcat deployment method

  1. Make sure that <scope>provided</scope>is not commented out.

    The Spring Boot embedded Servlet API runtime is provided by tomcat and needs to be excluded at runtime.

  2. Modifying the SpringBootLesson4Application class requires inheriting the org.springframework.boot.web.support.SpringBootServletInitializer and implementing its configure method, specifically return builder.sources (class name of this class); (Note that this cannot be here, this is an instance of a class, and xxx.class is a module of a class)Board).

    If the external container is deployed, instead of relying on the main method of the Application to start, it depends on a servlet-like web.xml to start Spring's application context.At this point, you need to inherit SpringBootServletInitializer and implement the configure method.

  3. Packaging: maven clean package
  4. Place the webapp directory under tomcat and rename it.

  1. Access address: http://localhost:8080/jsp-in-spring-boot

    The following pages can be accessed successfully in all three ways.

6 Question Description

When deploying external tomcat, the SpringBootServletInitializer class must be inherited, and if not, a 404 error will be reported, as shown on the following page.

There are two examples of this code.

Tags: Java Spring JSP Tomcat Maven

Posted on Fri, 06 Sep 2019 19:59:24 -0700 by xenophobia