Spring Boot项目无WEB-INF文件夹?JSP存放路径及手动创建可行性咨询
Hey there! This is a super common question when starting out with Spring Boot and JSPs—since Spring Boot leans hard into convention over configuration, the traditional WEB-INF folder isn't included in the default project structure generated by Spring Initializr. Let's break this down clearly:
1. Where to place your JSP files
Spring Boot looks for JSPs in two standard locations by default:
src/main/resources/META-INF/resources/WEB-INF/jsp/src/main/webapp/WEB-INF/jsp/
The second option (src/main/webapp) is the more traditional approach and what most developers use. The catch? This directory doesn't exist in the default Initializr project, so you'll need to create it manually (more on that below).
Whichever location you choose, make sure to configure your view resolver in application.properties (or application.yml) to point to the right path:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
This tells Spring where to find your JSP files when resolving view names.
2. Can you manually create the WEB-INF folder?
Absolutely—this is not only allowed, but it's the recommended approach when working with JSPs or other traditional web resources (like tag libraries, static assets, or custom error pages). Here's how to set it up:
- In your project root, create the
src/main/webappdirectory (if it doesn't already exist) - Inside
webapp, create theWEB-INFsubfolder - Add a
jspfolder insideWEB-INFand drop your JSP files there
Critical Note: Don't forget the dependencies
For JSPs to work in Spring Boot, you need to include two key dependencies in your pom.xml (Maven) or build.gradle (Gradle):
Maven:
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
Gradle:
implementation 'javax.servlet:jstl' providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
Without these, even if your JSPs are in the right place, Spring Boot won't be able to render them.
Hope that solves your issue! If you run into any trouble getting your JSPs to load, double-check the view resolver config and dependencies first—those are the most common pain points.
内容的提问来源于stack exchange,提问作者Rajanikanta Pradhan

