博主在使用sring-boot跳转HTML页面后,由于好奇心就想跳转到JSP页面,就在网上搜相关信息,结果不是跳转500错误就是下载JSP文件。各种坑啊,在博主跳了N多坑后,终于跳转JSP页面成功。故写此文章便于使用到的小伙伴不再进坑。

1、新建spring-boot项目  目录结构如下

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

SpringBoot访问不了JSP但却能进入后台 随笔 第1张

2、新建TestController.java文件,内容如下

 

package com.example.controller;

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

@Controller
public class TestController {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

3、新建webapp文件夹,与resources同级。

SpringBoot访问不了JSP但却能进入后台 随笔 第2张

4、新建JSP页面,此时发现New里面没有JSP页面。需要设置一下才会出现哟。

SpringBoot访问不了JSP但却能进入后台 随笔 第3张

5、点击File->Project Structure...

SpringBoot访问不了JSP但却能进入后台 随笔 第4张

6、点击Modules->绿色加号->Web

SpringBoot访问不了JSP但却能进入后台 随笔 第5张

7、双击此处

SpringBoot访问不了JSP但却能进入后台 随笔 第6张

8、选择刚刚新建的webapp,点击OK,继续OK。

SpringBoot访问不了JSP但却能进入后台 随笔 第7张

9、此时webapp上有个蓝色圆点表示设置成功。

SpringBoot访问不了JSP但却能进入后台 随笔 第8张

10、在webapp上单击右键New,此时出现JSP文件。

SpringBoot访问不了JSP但却能进入后台 随笔 第9张

11、新建index.jsp

SpringBoot访问不了JSP但却能进入后台 随笔 第10张

12、index.jsp内容

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1 style="color: red">Hello World</h1>
</body>
</html>

13、新建MyWebAppConfigurer类

SpringBoot访问不了JSP但却能进入后台 随笔 第11张

 

14、MyWebAppConfigurer内容

 

package com.example.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

}

15、在pom.xml中加入依赖JAR包

 

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <version>7.0.59</version>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

16、启动Application,访问127.0.0.1:8080/index

SpringBoot访问不了JSP但却能进入后台 随笔 第12张

17、跳转完成。

 

以上就是spring-boot跳转JSP页面的过程,下面说说跳转遇到的坑。

一、缺少依赖JAR包

 

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

跳转失败

SpringBoot访问不了JSP但却能进入后台 随笔 第13张

提示还算明确,缺少jstl标签

二、使用provided版本JSP解析器JAR包,

 

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
</dependency>

下载JSP文件

SpringBoot访问不了JSP但却能进入后台 随笔 第14张

改为

 

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <version>7.0.59</version>
</dependency>

问题解决,至于为什么provided版本的不行,感兴趣的小伙伴可以深究下,留言给我。

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄