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
原文地址:https://www.cnblogs.com/cslj2013/p/10852272.html