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


    <!-- JSP rendering engine -->
        <!-- If it is external tomcat If deployed, provided Property needs to be turned on -->


    <!-- JSTL -->


2 Controller Code

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 configuration

#service Port (Dev)

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

application.hello = hello jsp on spring boot

4 Jsp Page Code

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


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.

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