我写这个博客是因为我也碰到了问题,网上搜答案,也无果,最后我对比了一下能运行起来的项目和运行不起来的项目,发现了一点不同,
运行起来的项目,编译的时候,application.properties编译进去了,
跑不起来的项目,application.properties没编译进去
不过还不仅仅是这些,还有pom文件里面有一个小细节,就是光配置了webapp编译路径
<resource>
<!--源文夹-->
<directory>src/main/webapp</directory>
<!--指定编译到META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<!--指定源文件夹中的哪个资源要编译进行-->
<includes>
<include>*.*</include>
</includes>
</resource>
没有配置resources
这样配就可以了
<resources>
<resource>
<!--源文夹-->
<directory>src/main/webapp</directory>
<!--指定编译到META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<!--指定源文件夹中的哪个资源要编译进行-->
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
其它的不变,下面是具体的步骤,前提是已经创建好一个springboot项目
第一步,在pom文件中引入SpringBoot内嵌Tomcat对jsp的解析依赖,不添加解析不了jsp
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
指定jsp编译路径和配置编译核心配置文件的路径,两个都要有,缺一不可
<resources>
<resource>
<!--源文夹-->
<directory>src/main/webapp</directory>
<!--指定编译到META-INF/resources-->
<targetPath>META-INF/resources</targetPath>
<!--指定源文件夹中的哪个资源要编译进行-->
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
第二步:创建webapp文件夹,在设置中设置webapp文件夹,点击➕号,设置为webapp文件夹
第三步:在核心配置文件application.properties中配置视图解析器
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
第四步:创建controller类,写方法和创建jsp页面
就可以了,剩下的就是运行项目了