springboot learning (5) spring boot integration jsp

Recently, I am learning springboot. According to the blogs of the great gods on the Internet and the examples of the official website, I have experienced various difficulties and obstacles of Xiaobai, but have not yet achieved the desired effect. Spring boot integrates jsp, but it's enough for me to search for learning materials, but I'm not willing to directly enlarge the code of the gods, so I have a little more pits.

  1. pom.xml needs to introduce relevant support
    <?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>
    
    	<groupId>com.example</groupId>
    	<artifactId>spring-boot-sample-jsp1</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>war</packaging>
    
    	<name>spring-boot-sample-jsp1</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.7.RELEASE</version>
    		<relativePath /> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<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.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    		<!--Add support for tomcat-->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-tomcat</artifactId>
    			<scope>provided</scope>
    		</dependency>
    
    		<!--Support for jsp-->
    		<dependency>
    			<groupId>org.apache.tomcat.embed</groupId>
    			<artifactId>tomcat-embed-jasper</artifactId>
    			<scope>required</scope>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    
     
  2. To test the controller, you need to pay attention to the annotation here is @Controller, not @RestController, otherwise it will not return to the page no matter how it is configured, the latter returns the content, you can go to self-study to understand
    package com.example.springbootsamplejsp1;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Function Description
     *@date September 22, 2017 at 3:00:06 PM
     * @author wusj
     * @version 1.0
     */
    @Controller
    public class TestController {
    
        @RequestMapping("/index")
        public String index(Map<String,Object> map){
            map.put("name", "wusj");
            return "index";
        }
    }
    
  3. When creating a new maven project, you need to select war instead of jar. I don't know how to get the jar package. I was right at the beginning, that is, the project is a jar package, not a war package, and it keeps reporting errors: This application has no explicit mapping for /error, so you are seeing this as a fallback
  4. The startup class needs to inherit SpringBootServletInitializer, or write a separate class to inherit it ServletInitializer.java
    package com.example.springbootsamplejsp1;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    import com.example.SpringBootSampleJsp1Application;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
    	@Override
    	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    		return application.sources(SpringBootSampleJsp1Application.class);
    	}
    
    }
    
  5. The configuration file needs to configure related information
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
  6. If you have done all the above and still can't come out, the preliminary judgment may be that rp is not as good as me.
  7. I hope you don't take detours, hee hee. The project structure is as follows

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485866&siteId=291194637