Spring Boot配置缺失问题求助:附启动类与配置类代码
Hey there! It looks like you're hitting a few common pitfalls when setting up JSP and default Servlet handling in Spring Boot. Let's break down what's missing and fix your code step by step.
First, Let's Address the Key Issues in Your Current Code
@EnableWebMvcconflicts with Spring Boot's auto-configuration
Spring Boot automatically sets up Web MVC for you out of the box. Adding@EnableWebMvcdisables all those default settings, which is probably why your JSP parsing isn't working as expected. You can safely remove this annotation.WebMvcConfigurerAdapteris outdated
Since Spring 5, this class has been deprecated. Instead, you should directly implement theWebMvcConfigurerinterface (it has default methods, so you don't need to override every method).Missing JSP dependencies
Spring Boot's web starter doesn't include JSP support by default. You'll need to add specific dependencies to make JSPs work with the embedded Tomcat server.
Corrected SpringConfig.java
Here's the updated configuration class that fixes these issues:
@Configuration // Remove @EnableWebMvc to let Spring Boot's auto-config do its job // @ComponentScan is redundant because @SpringBootApplication already scans your base packages public class SpringConfig implements WebMvcConfigurer { // Configure JSP view resolver @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); // Adjust this to your actual JSP file path resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); // Required if you use JSTL tags return resolver; } // Enable default Servlet handling (Spring Boot auto-configures this, but you can customize it here if needed) @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
Add Required Dependencies
For Maven Projects (add to pom.xml):
<dependencies> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- JSP support for embedded Tomcat --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- JSTL for JSP tag libraries --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <!-- Set packaging to WAR (JSPs don't work with JAR packaging) --> <packaging>war</packaging>
For Gradle Projects (add to build.gradle):
plugins { id 'org.springframework.boot' version 'your-spring-boot-version' id 'war' // Required for JSP support } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper' implementation 'javax.servlet:jstl' }
Keep Your Application.java As-Is
Your main startup class is already correct—no changes needed here:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Quick Notes to Remember
- JSP File Location: Place your JSP files in
src/main/webapp/WEB-INF/views/(or adjust theprefixinviewResolverto match your setup). - Packaging Type: JSPs won't work with JAR packaging—you must use WAR packaging for this setup.
- Testing: After making these changes, restart your application and try accessing a JSP endpoint to verify everything works.
内容的提问来源于stack exchange,提问作者mihai maier

