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

Spring Boot打包为Jar时JSP页面无法显示的技术求助

Spring Boot JSP无法显示?这些配置你绝对漏了!

Hey there! I’ve stumbled on this exact JSP display issue with Spring Boot before, so let me walk you through the most common missing configurations and steps that usually fix it—since your DAO layer is working, the problem is almost certainly in the JSP setup.

1. Double-check your pom.xml dependencies

Spring Boot doesn’t include JSP support out of the box, so you need to add specific dependencies:

<!-- Embedded Tomcat support for JSP -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!-- If you're building a JAR (default), remove the `provided` scope -->
    <!-- If you're building a WAR, keep `provided` since external Tomcat provides this -->
    <scope>compile</scope>
</dependency>
<!-- Add this if you use JSTL tags (even for static pages, it's safe to include) -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

A super common mistake is leaving tomcat-embed-jasper as provided when running a JAR—it’ll cause the app to fail to find JSP rendering classes at runtime.

2. Fix your project structure (critical!)

Spring Boot has strict rules for where JSP files live—don’t put them in src/main/resources/templates (that’s for Thymeleaf/Freemarker). The correct structure is:

src
├── main
│   ├── java
│   │   └── com/yourpackage/ (main app class, controllers, DAOs)
│   ├── resources
│   │   └── application.properties
│   └── webapp
│       ├── WEB-INF
│       │   └── jsp
│       │       └── your-static-page.jsp (your JSP goes here)
│       └── (optional: css/js/img go directly under webapp)

If you want to use a custom directory (like WEB-INF/views), you’ll need to adjust the view resolver config below.

3. Configure the view resolver in application.properties

Tell Spring Boot where to find your JSP files and how to render them. Add these lines to your application.properties:

# Prefix = path to your JSP directory
spring.mvc.view.prefix=/WEB-INF/jsp/
# Suffix = file extension of your views
spring.mvc.view.suffix=.jsp

For example, if your JSP is named home.jsp in WEB-INF/jsp, returning "home" from your controller will map directly to that file.

4. Make sure your controller is set up correctly

Even for static pages, your controller needs to use @Controller (not @RestController—that returns raw text/JSON, not views):

@Controller
public class ProductController {
    @GetMapping("/show-static-jsp")
    public String displayStaticPage() {
        // Return the NAME of the JSP file (without .jsp suffix)
        return "your-static-page";
    }
}

Visit http://localhost:8080/show-static-jsp after starting the app to test this.

5. Watch out for packaging quirks

  • If you’re building a WAR file: Make sure your main application class extends SpringBootServletInitializer to enable deployment on external Tomcat:
@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
        return app.sources(YourApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}
  • If you’re building a JAR file: Ensure the tomcat-embed-jasper dependency is set to compile (not provided) as noted earlier.

Troubleshooting tip

If it still doesn’t work, check your console logs for errors like Could not resolve view with name 'your-page'. That usually means either:

  • Your JSP file is in the wrong directory
  • The view resolver prefix/suffix is misconfigured
  • You used @RestController instead of @Controller

内容的提问来源于stack exchange,提问作者AlxT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:43:10