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. Python Selenium Webdriver常用方法总结

    Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...

  2. libusb: android上集成libusb库

    1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...

  3. 清除JAVA 项目中的注释

    package com.lookcoder.inschool.utils; import java.io.BufferedReader; import java.io.File; import jav ...

  4. python调用kafka服务(使用kafka-python库)

    试验环境: CDH 5.15.1 CentOS 7 Python 3.7.0 kafka 1.1.1 kafka-python :https://pypi.org/project/kafka-pyth ...

  5. Bladex使用代码生成器操作步骤

    一.从私服上下载BladeX和Saber 二.运行BladeX所有服务 三.运行Saber 四.数据库创建自己需要使用的表(建议表名和字段名为:bldex_xxxx,xxx_xxxx,不要使用驼峰命名 ...

  6. Delphi ADOConnection连接

    unit Unit_DM; interface usesSysUtils, Classes, DB, ADODB,inifiles,windows,forms,controls; typeTDM = ...

  7. MySQL执行计划值type,强烈推荐

    表结构: create table user ( id int primary key, name varchar(), sex varchar(), index(name) )engine=inno ...

  8. 使用Centos7.5+Nginx+Gunicorn+Django+Python3部署blog项目

    项目开发环境是 Python3.5.2+Django1.10.6+Sqlite3+Centos7.5+Nginx1.12.2+Gunicorn 发布出来供需要的同学借鉴参考.文中如有错误请多多指正! ...

  9. zookeeper ACL权限

    原文链接:https://www.jianshu.com/p/392248ab27f4 对zookeeper设置ACL属性 我们以zkCli为例,来说明zookeeper对ACL的设置. 使用zkCl ...

  10. HTTP协议:从原理到流程|乐字节

    这次给大家带来的是HTTP协议:从原理到流程的详解 一.HTTP 协议 HTTP 协议(Hypertext Transfer Protocol, 超文本传输协议),是一个客户端请求和回应的 标准协议, ...