• Using JSP as a view layer technology is not recommended in a Spring Boot project
  • Jar packages cannot access JSP resources, war packages can

Add the dependent

The old Web project was a Tomcat container where the project was deployed, tomcat was responsible for compiling the JSPS into servlets and executing them, and the sentence JSP engine was responsible for handling the process. Although There is a Tomcat container embedded in SpringBoot, there is no JSP engine, so SpringBoot embedded Tomcat cannot handle JSPS. If you want to use JSP in your SpringBoot project, you must add dependencies to the JSP engine;

<! -- Parsing JSP library -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<build>
    <! -- SpringBoot requires that JSP files must be compiled into the specified meta-INF /resources directory, otherwise access will not be possible -->
    <resources>
        <resource>
            <! -- source file location -->
            <directory>src/main/webapp</directory>
            <! META-INF/resources --> compile to META-INF/resources
            <targetPath>META-INF/resources</targetPath>
            <! -- specifies which files to compile into, ** denotes the webapp directory and subdirectories, *.* denotes all files -->
            <includes>
                <include>/ *. * * *</include>
            </includes>
        </resource>
    </resources>
</build>
Copy the code

After adding dependencies, you should consider what directory the JSP should be stored in: JSP pages should be stored in the “webapp” directory web-INF; The WebApp directory should be in the main directory at the same level as the Java and Resources directories

The Idea of integration

  1. Select the Project –>File–>Project Structure–>Modules, select the Project name, then select Web, then click “+” under Web Resource Directories and specify WebApp as the path.

  2. In the middle of the process, it will prompt whether to create folders, just confirm it, and finally there will be multiple WebApp folders, the effect is as follows: ↓

  3. To specify the SpringBoot boot directory, add **$ContentRoot$**

  4. Set up the application. The properties

    By default, there are other folders under webApp, add folder/below
    spring.mvc.view.prefix=/
    # Default page suffix directory
    spring.mvc.view.suffix=.jsp
    Copy the code
  5. When you specify WebApp as a directory that supports Web technologies, a dot is added to the directory icon, and a shortcut to create a JSP page is created by right-clicking

  6. Is it feasible to test

    1. Create demo.jsp under WebApp

      <%@ page contentType="text/html; charset=UTF-8" language="java" %>
      <html>
          <head>
              <title>Title</title>
          </head>
          <body>
              ${title}
          </body>
      </html>
      Copy the code
    2. Writing the controller

      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.GetMapping;
      
      @Controller
      public class ViewController {
          @GetMapping("demo")
          public String demo(Model model){
              model.addAttribute("title"."Use JSP integration in SpringBoot");
              return "demo"; }}Copy the code

The Eclipse integrated

Using Eclipse to integrate JSP is much simpler than idea, so continue.

  1. Set application.properties(or an application.yml file, but in a different way)

    By default, there are other folders under webApp, add folder/below
    spring.mvc.view.prefix=/
    # Default page suffix directory
    spring.mvc.view.suffix=.jsp
    Copy the code
  2. Creating a startup class

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class JspApplication {
    	public static void main(String[] args) { SpringApplication.run(JspApplication.class,args); }}Copy the code
  3. Create demo.jsp under WebApp (this is just to demonstrate the functionality, it is best to create folder stores according to the functionality in the formal project)

    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    <html>
        <head>
            <title>Title</title>
        </head>
        <body>
            ${title}
        </body>
    </html>
    Copy the code
  4. Writing the controller

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class ViewController {
        @GetMapping("demo")
        public String demo(Model model){
            model.addAttribute("title"."Use JSP integration in SpringBoot");
            return "demo"; }}Copy the code
  5. Right-click the JspApplication. Java, input url: http://localhost:8080/demo