SpringBoot访问不了JSP但却能进入后台
博主在使用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但却能进入后台的更多相关文章
- springboot同时支持访问html5和jsp时,导致后台ResponseBody返回中文乱码
		背景:原系统是由springboot jsp,所有访问都是jsp 现在需要做HTML5定位,要同时支持访问HTML5和JSP 在application.yml的spring标签下配置 mvc: #vi ... 
- SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP
		SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ... 
- 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:标记红色处为重点 ... 
- springBoot整合mybatis、jsp 或  HTML
		springBoot整合mybatis.jsp Spring Boot的主要优点: 1: 为所有Spring开发者更快的入门: 2: 开箱即用,提供各种默认配置来简化项目配置: 3: 内嵌式容器 ... 
- SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合
		SpringBoot访问NoSQL SpringBoot访问Redis 在pom.xml添加boot-data-redis定义 <parent> <groupId>org.sp ... 
- 玩转 SpringBoot 2 快速整合 | JSP 篇
		前言 JavaServer Pages(JSP)技术使Web开发人员和设计人员能够快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面. 作为Java技术系列的一部分,JSP技术可以快速开发独 ... 
- idea环境下SpringBoot Web应用引入JSP
		1. 环境 开发环境:idea2019.3 jkd版本:1.8 springboot版本:2.6.2 2. 引入JSP的步骤 2.1 新建工程,引入依赖 这里只是解析jsp,因此只需要引入spring ... 
- SpringMVC 无法访问到指定jsp页面可能的原因
		当出现你的程序可以访问到对应的controller层.但是却无法访问对应的jsp文件时.你首先做的不是检查web.xml等配置文件,而是打开的服务器根文件检查对应路径下的文件是否存在.命名是否正确.命 ... 
- 访问WEB-INF下JSP资源的几种方式(转)
		访问WEB-INF下JSP资源的几种方式 方法一: 本来WEB-INF中的jsp就是无法通过地址栏访问的,所以安全. 如果说你要访问这个文件夹中的jsp文件需要在项目的web.xml文件中去配置ser ... 
随机推荐
- UEditor 在 Layer 模态框中无法使用问题
			问题: 解决方法: 在 使用 ueditor 的页面顶部加入js代码: window.UEDITOR_HOME_URL = "__STATIC__/path/to/ueditor/&quo ... 
- mybatis中如何将多个表的查询结果,放入结果集中返回
			1.首先需要将resultMap进行改造,为了避免对其他sql的影响建议另外定义一个resultMapExtral,避免id相同, 2.然后在resultMapExtral中添加其它表的字段,若多个表 ... 
- python中 "is"和"=="的区别
			python中"is"和"=="区别 在做leetcode的时候,在判断两个数据是否相等时使用了python中的is not,想着入乡随俗,既然入了python ... 
- Codeforces D. Color the Fence(贪心)
			题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ... 
- Easy sssp(spfa判负环与求最短路)
			#include<bits/stdc++.h> using namespace std; int n,m,s; struct node{ int to,next,w; }e[]; bool ... 
- [Exception] java.util.MissingFormatArgumentException
			java.util.MissingFormatArgumentException: Format specifier 's' at java.util.Formatter.format(Formatt ... 
- 忘记 MySQL 的 root 帐号密码该怎么办
			如果你忘了 MySQL 的 root 帐号密码,别担心,使用下面步骤就可以重设一个新密码: 首先停止 MySQL 服务 “/etc/init.d/mysql stop” 启动 MySQL 服务并屏蔽用 ... 
- keras模块学习之层(layer)的使用-笔记
			本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! keras的层主要包括: 常用层(Core).卷积层(Convolutional).池化层(Pooling).局部连接层.递归层(R ... 
- machine learning (5)---learning rate
			degugging:make sure gradient descent is working correctly cost function(J(θ)) of Number of iteration ... 
- (java)selenium webdriver爬虫学习--爬取阿里指数网站的每个分类的top50 相关数据;
			主题:java 爬虫--爬取'阿里指数'网站的每个分类的top50 相关数据: 网站网址为:http://index.1688.com/alizs/top.htm?curType=offer& ... 
