You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot配置缺失问题求助:附启动类与配置类代码

Fixing Your Spring Boot JSP & Default Servlet Configuration

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

  1. @EnableWebMvc conflicts with Spring Boot's auto-configuration
    Spring Boot automatically sets up Web MVC for you out of the box. Adding @EnableWebMvc disables all those default settings, which is probably why your JSP parsing isn't working as expected. You can safely remove this annotation.

  2. WebMvcConfigurerAdapter is outdated
    Since Spring 5, this class has been deprecated. Instead, you should directly implement the WebMvcConfigurer interface (it has default methods, so you don't need to override every method).

  3. 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 the prefix in viewResolver to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:08:33