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

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

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同级。

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

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

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

7、双击此处

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

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

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

11、新建index.jsp

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类

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

17、跳转完成。

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

一、缺少依赖JAR包

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

跳转失败

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

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

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

下载JSP文件

改为

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

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

SpringBoot访问不了JSP但却能进入后台的更多相关文章

  1. springboot同时支持访问html5和jsp时,导致后台ResponseBody返回中文乱码

    背景:原系统是由springboot jsp,所有访问都是jsp 现在需要做HTML5定位,要同时支持访问HTML5和JSP 在application.yml的spring标签下配置 mvc: #vi ...

  2. SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP

    SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...

  3. SpringBoot 访问jsp文件报错Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/welcome.jsp]的解决办法

    由于SpringBoot不在支持jsp,所以想使用jsp的情况下需要在pom.xml配置servlet依赖,jstl标签库和tomcat依赖.以下是我的pom.xml的配置: (ps:标记红色处为重点 ...

  4. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  5. SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合

    SpringBoot访问NoSQL SpringBoot访问Redis 在pom.xml添加boot-data-redis定义 <parent> <groupId>org.sp ...

  6. 玩转 SpringBoot 2 快速整合 | JSP 篇

    前言 JavaServer Pages(JSP)技术使Web开发人员和设计人员能够快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面. 作为Java技术系列的一部分,JSP技术可以快速开发独 ...

  7. idea环境下SpringBoot Web应用引入JSP

    1. 环境 开发环境:idea2019.3 jkd版本:1.8 springboot版本:2.6.2 2. 引入JSP的步骤 2.1 新建工程,引入依赖 这里只是解析jsp,因此只需要引入spring ...

  8. SpringMVC 无法访问到指定jsp页面可能的原因

    当出现你的程序可以访问到对应的controller层.但是却无法访问对应的jsp文件时.你首先做的不是检查web.xml等配置文件,而是打开的服务器根文件检查对应路径下的文件是否存在.命名是否正确.命 ...

  9. 访问WEB-INF下JSP资源的几种方式(转)

    访问WEB-INF下JSP资源的几种方式 方法一: 本来WEB-INF中的jsp就是无法通过地址栏访问的,所以安全. 如果说你要访问这个文件夹中的jsp文件需要在项目的web.xml文件中去配置ser ...

随机推荐

  1. iview表单数字验证

    sort: [ {required: true, message: '请填写栏目排序', trigger: 'blur'}, {type: 'number', message: '请输入数字', tr ...

  2. 【HICP Gauss】数据库 环境的搭建 -1

    1.安装规则    1.主机名必须网络唯一  2.主机名必须两位数以上 可以中划线 不能下划线  3.固定IP地址  4.端口号 1888 新增账户 omm 用户组 dbgrp ,家目录 /home/ ...

  3. Redis一主二从Sentinel监控配置

    本文基于Redis单实例安装安装.https://gper.club/articles/7e7e7f7ff7g5egc4g6b 开启哨兵模式,至少需要3个Sentinel实例(奇数个,否则无法选举Le ...

  4. redis问题解决 Caused by: io.lettuce.core.RedisException: io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specifie

    1找到redis的配置文件 redis.conf vim redis.conf 修改 protected-mode  yes 改为 protected-mode no 注释掉 #bin 127.0.0 ...

  5. Multiple declaration for 'fd_set'

    如果程序编译时报 [C++ Error] winsock2.h(109): E2238 Multiple declaration for 'fd_set' 这样的错误,可以尝试在,winsock2.h ...

  6. 《Exception团队》第七次作业:团队项目设计完善&编码

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件编码实现的工程要求 作业任务 1. ...

  7. 从GITHUB下载源码

    从昨天开始就想着从GitHub上下载一个开源的Vue的实战项目,希望能从中学习更多的Vue的实用内容,结果搞了半天好不容易下载了,不知道怎么弄.然而,今天终于成功了,激动地我赶紧来记录一下. 如何从G ...

  8. Hibernate的持久化对象

     Hibernate的持久化类 什么是持久化类        1. 持久化类:就是一个Java类(咱们编写的JavaBean),这个Java类与表建立了映射关系就可以成为是持久化类.        * ...

  9. Backpack III

    Description Given n kinds of items, and each kind of item has an infinite number available. The i-th ...

  10. [Javascript] Avoid Accidental Returns of New State by using the void Keyword

    For example we have a 'useState' function, which takes a state and a function to update the state: c ...