SpringBoot 不建议使用jsp,但是在做项目的时候可能还需要使用jsp。如果使用idea有的时候不能访问webapp目录,因此下边的jsp也就是不能被访问到了,出现404错误。
如果是单独的一个项目是没有问题,按照如下修改就行了。
第一:属性配置文件application.properties 增加
#页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ #页面默认后缀目录 spring.mvc.view.suffix=.jsp
第二步:
pom。xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jsp的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<!-- JSTL (JSP standard Tag Library) JSP 标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Tomcat的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
第三步:
按照图修改如下

按照如上步骤单独的项目是没有问题的
如果一个项目下模块是web,采用这个方式直接运行依然找不到webapp目录,采取的方式是如下:
创建一个类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
//import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class TomcatConfig {
@Value("${bw.factory.doc.root}")
private String rootDoc;
@Bean
public AbstractServletWebServerFactory embeddedServletContainerFactory() {
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
tomcatServletWebServerFactory.setDocumentRoot(
new File(rootDoc));
return tomcatServletWebServerFactory;
}
}
属性文件配置:
bw.factory.doc.root=bw.factory.doc.root=D:/project/smallfile/all/all_consumer/src/main/webapp
这样怎样运行都不会出问题的,完美解决
本文详细介绍如何在SpringBoot项目中正确配置并使用JSP页面,包括修改属性配置文件、pom.xml依赖添加及自定义Tomcat配置,确保webapp目录下的JSP资源能被正确访问。
3306

被折叠的 条评论
为什么被折叠?



