springboot对jsp支持不友好,内部tomcat对jsp不支持,需要使用外部tomcat,且必须打包为war包。

1 创建maven项目

注意:必须为war类型,否则找不到页面。

且不要把jsp页面存放在resources(原因:可能被别人访问,其次不在classes类路径中),因此,一般自行创建目录存放(一般/WEB-INF/下。

 2 pom文件

<packaging>war</packaging> <!-- 注意为war包!!! -->

   <!-- spring-boot-starter-parent 整合第三方常用框架依赖信息(包含各种依赖信息) -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent> <!-- spring-boot-starter-web springboot整合springmvc web
实现原理:maven依赖继承关系,相当于把第三方常用maven依赖信息,在parent项目中已封装
-->
<dependencies>
<!-- 根据需要选择parent中封装的第三方框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 不需要写版本号,因为在parent中已封装好版本号 -->
</dependency>
<!-- tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- SpringBoot 外部tomcat支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 解决No Java compiler available for configuration options compilerClassName报错
https://www.mkyong.com/spring-boot/spring-boot-web-jsp-no-java-compiler-available/
-->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

注意点:

1)<packaging>war</packaging> <!-- 注意为war包!!! -->

2)No Java compiler available for configuration options compilerClassName报错 :

解决办法引入ecj依赖

<dependency>
  <groupId>org.eclipse.jdt.core.compiler</groupId>
  <artifactId>ecj</artifactId>
  <version>4.6.1</version>
  <scope>provided</scope>
 </dependency>

3 创建application.properties

#springmvc 配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

4 在webapp目录下创建WEB-INF/jsp

如上图,新建index.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
springboot 整合 jsp
</body>
</html>

5 创建JspController类和启动类JspApp

@SpringBootApplication
public class JspApp { public static void main(String[] args) {
SpringApplication.run(JspApp.class, args);
} }
@Controller //注意不能使用@RestController(无视图层),否则无法找到对应的jsp页面
public class JspController { @RequestMapping("/index")
public String index() {
return "index";//对应jsp文件名称即index.jsp
} }

6 运行启动类JspApp

访问:http://localhost:8080/index

启动后访问报错:

java.lang.IllegalStateException: No Java compiler available for configuration options compilerClassName: [null] and compiler: [null]

参考:https://www.cnblogs.com/ming-blogs/p/10283764.html

添加

<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>

依赖即可

再次访问,页面返回springboot 整合 jsp

https://www.mkyong.com/spring-boot/spring-boot-web-jsp-no-java-compiler-available/

git代码:https://github.com/cslj2013/springboot2.0_for_jsp.git

springboot学习入门简易版五---springboot2.0整合jsp(11)的更多相关文章

  1. springboot学习入门简易版六---springboot2.0整合全局捕获异常及log4j日志(12-13)

    使用Aop实现 1创建异常请求 在原有项目基础上,jspController中创建一个可能发生异常的请求: /** * 全局捕获异常测试 * @param i * @return */ @Reques ...

  2. springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)

    一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...

  3. springboot学习入门简易版三---springboot2.0启动方式

    2.4使用@componentscan方式启动 2.4.1 @EnableAutoConfiguration 默认只扫描当前类 @EnableAutoConfiguration 默认只扫描当前类,如果 ...

  4. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

  5. springboot学习入门简易版八---springboot2.0多环境配置、整合mybatis mysql8+(19-20)

    2.11 SpringBoot多环境配置(19)  application.properties中配置 Spring.profiles.active=prd 配置环境: Application-dev ...

  6. springboot学习入门简易版四---springboot2.0静态资源访问及整合freemarker视图层

    2.4.4 SpringBoot静态资源访问(9) Springboot默认提供静态资源目录位置需放在classpath下,目录名需要符合如下规则 /static  /public  /resourc ...

  7. springboot学习入门简易版一---springboot2.0介绍

    1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...

  8. springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)

    1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 ...

  9. eclipse 中springboot2.0整合jsp 出现No Java compiler available for configuration options compilerClassName

    今天使用eclipse创建springboot整合jsp出现一个问题,在idea中并没有遇到这个问题.最后发现是需要在eclipse中添加一个eclipse依赖,依赖如下: <dependenc ...

随机推荐

  1. 压测引起的 nginx报错 502 no live upstreams while connecting to upstream解决

    对系统的某个接口进行极限压测,随着并发量上升,nginx开始出现502 no live upstreams while connecting to upstream的报错,维持最大并发量一段时间,发现 ...

  2. PostgreSQL递归查询示例

    PostgreSQL提供了WITH语句,允许你构造用于查询的辅助语句.这些语句通常称为公共表表达式或cte.cte类似于只在查询执行期间存在的临时表. 递归查询是指递归CTE的查询.递归查询在很多情况 ...

  3. centos 宝塔面版 运行 thinkjs

    centos 宝塔面版 运行 thinkjs 几点要注意的地方: 1.  https ssl 如图 2. thinkjs 运行子目录在/www如图配置: 3. 代理配置(展示查看配置) server ...

  4. 【Git】.git/FETCH_HEAD: Permission denied 的解决方法

    背景: 用webhook去拉取代码.报错 .git/FETCH_HEAD: Permission denied 原因分析:.git/FETCH_HEAD的这个文件所属组和所属主是root权限,而我用w ...

  5. [Bayes] Concept Search and LDA

    重要的是通过实践更深入地了解贝叶斯思想,先浅浅地了解下LDA. 相关数学知识 LDA-math-MCMC 和 Gibbs Sampling LDA-math - 认识 Beta/Dirichlet 分 ...

  6. Python中解决递归限制的问题

    在做某些算法时,使用递归会出现类似下面的报错: RuntimeError: maximum recursion depth exceeded python默认的递归深度是很有限的,大概是900多的样子 ...

  7. [LeetCode] 354. Russian Doll Envelopes 俄罗斯套娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  8. 【SSH进阶之路】Spring的AOP逐层深入——采用注解完成AOP(七)

    上篇博文[SSH进阶之路]Spring的AOP逐层深入——AOP的基本原理(六),我们介绍了AOP的基本原理,以及5种通知的类型, AOP的两种配置方式:XML配置和Aspectj注解方式. 这篇我们 ...

  9. Kubernetes 配置管理 Dashboard(十三)

    目录 一.安装配置 1.1 下载 镜像 1.2.安装 1.3.修改 NodePort 二.配置授权 Kubernetes 所有的操作我们都是通过命令行工具 kubectl 完成的.为了提供更丰富的用户 ...

  10. OpenJudge 2755:神奇的口袋

    总时间限制: 10000ms 内存限制: 65536kB 描述 有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体 ...