今天就总结下一直以来遇到的IDEA 环境下springboot 集成jsp (内嵌Tomcat )运行时遇到的问题:
第一个问题: 404 找不到jsp页面
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
No message available
解决方法:
1. 加入解析jsp 的相关依赖:
<!-- 引入springboot内嵌的tomcat 对jsp的解析--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 引入servlet 依赖的jar包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- 引入jsp 依赖的jar包--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <!-- 引入jstl 依赖的jar包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
加入相关的资源
<resources> <resource> <!-- 将src/main/java下的xml 文件编译到classes下--> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource><!-- 将src/main/resources下的所有文件文件编译到classes下--> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource><!-- 将src/main/webapp下的所有文件文件编译到classes/META-INF/resources下--> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/*.*</include> </includes> </resource> </resources>
2. 点击运行的Main类,选择“Edit Configurations” , 点击左边您的主程序入口类, 在右边的“working directory” 选择$MODULE_DIR$ .
第二个问题: 将项目使用内嵌tomcat 打成JAR包使用java -jar 名称.jar 运行时,运动如下问题
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
检查了一下,发现javax.el.ExpressionFactory.newInstance()这个方法是EL2.2版本之后才有的方法,而在EL2.1之中是没有这个方法的,问题很明显:org.apache.jasper中试图调用2.2版本的EL,当时提供的EL的版本是2.1版本,所以解决的方式无非就是两个,要不降低org.apache.jasper的版本,要不提升el的版本。
修改pom.xml文件,将
在运行测试过程中还发现,在IDEA环境下,可以正常访问到JSP页面, 但是使用命令 部署JAR 包之后发现JSP无法正常访问404.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
原因是编译打包插件的版本问题,经过测试发现,只有1.4.2的版本是可行的。
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- 为打jar包指定版本号--> <version>1.4.2.RELEASE</version> </plugin> </plugins>
重新maven install 再次打包,命令运行 部署JAR 包之后发现JSP仍然无法正常访问404.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
在application.properties 配置文件中添加项目的根路径:
server.servlet.context-path=/springboot
再次打包部署,这次是可以访问到jsp页面了。