Springboot访问webapp下jsp出现404问题

我们在初学springboot搭建项目时可能会发现自己访问webapp下的jsp文件访问出现404。
如下图所示:
如果error page中也能找到你访问的路径,并且也能找到/WEB-INF下的jsp文件,这时候怎么办呢?
Springboot访问webapp下jsp出现404问题_第1张图片
首先我们要先知道,springboot是不太支持jsp的,我们需要先添加jsp的支持依赖包,检查一下有没有。

 
      org.apache.tomcat.embed  
      tomcat-embed-jasper  
      9.0.30 
      
     
      javax.servlet  
      jstl  
      1.2 
     

将自己的项目转web,没有的可以点击上面的“+”号添加
Springboot访问webapp下jsp出现404问题_第2张图片
更改所示路径到webapp
Springboot访问webapp下jsp出现404问题_第3张图片
在自己的application.yml文件中添加视图解析配置

mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .jsp
    static-path-pattern: /webapp/**
 resources:
    static-locations: classpath:/webapp

这个时候运行驱动类测试,如果还是出现404情况那么可能没有将webapp包打到MATE-INF/resource目录下,那么你就要在pom文件的build标签下添加如下:


      
        src/main/webapp
        META-INF/resources
        
          **/**
        
      
      
        src/main/resources
        true
        
          **/**
        
      
    

去写controller类

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Test {
    @RequestMapping("/getTest")
    public String getPage() {

        return "test";

    }
}

再去启动访问项目WEB-INF/pages下的jsp文件就可以了。
Springboot访问webapp下jsp出现404问题_第4张图片
这个时候就可以成功访问自己的jsp页面了。
Springboot访问webapp下jsp出现404问题_第5张图片

你可能感兴趣的:(springboot的坑)