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

Spring MVC应用JSP报404:WEB-INF下view目录未打包进target解决方案

Fixing JSP 404 Issue in Spring MVC (WEB-INF/view Not Included in Target)

Hey there! Let's tackle that 404 error you're hitting when accessing JSP pages. The core problem here is obvious—your WEB-INF/view folder isn't being packaged into the target directory during the Maven build. Let's walk through the fixes step by step.

1. Verify Your Project Directory Structure

Maven follows a strict standard layout for web projects, and if your JSPs aren't in the right spot, Maven won't automatically pick them up. Make sure your project structure matches this:

your-project/
├── src/
│   ├── main/
│   │   ├── java/          # Your controller/service classes live here
│   │   ├── resources/     # Configuration files (Spring XMLs, properties)
│   │   └── webapp/        # Critical directory for all web resources
│   │       ├── WEB-INF/
│   │       │   ├── view/  # Place your JSP files right here
│   │       │   └── web.xml (or Spring config files)
│   │       └── static/    # CSS, JS, images go here if you have them
│   └── test/
└── pom.xml

If your WEB-INF/view folder is tucked inside src/main/java or src/main/resources, move it to src/main/webapp/WEB-INF/ immediately—Maven doesn't treat those directories as web resource roots.

2. Add/Configure the Maven War Plugin

Your current pom.xml is missing the maven-war-plugin, which is responsible for packaging web resources into the final WAR file. Add this plugin configuration inside the <build><plugins> section of your pom:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.2</version> <!-- Use a recent stable version -->
            <configuration>
                <warSourceDirectory>src/main/webapp</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml> <!-- Skip if using Java-based config instead of web.xml -->
            </configuration>
        </plugin>
    </plugins>
</build>

This explicitly tells Maven where to find your web resources and ensures they're included in the build output.

3. Fix a Tiny Typo in Your Pom

I spotted a small but critical mistake in your aspectjweaver dependency—there's a Chinese comma in the version number:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.,</version> <!-- Replace the Chinese comma with a period, e.g., 1.8.13 -->
</dependency>

Fix that typo (use a valid version like 1.8.13) to avoid dependency resolution errors that could break your build process.

4. Rebuild Your Project

Run these Maven commands to clean old build artifacts and create a fresh WAR:

mvn clean package

Or, if you want to install the WAR into your local Maven repository:

mvn clean install

After this, check the target directory—your WEB-INF/view folder should now be present inside the WAR file (or exploded directory if you're running from an IDE).

5. Double-Check Spring MVC View Resolver

While we're at it, confirm your Spring MVC view resolver is pointing to the correct JSP location. Here's how to configure it:

XML Config:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
</bean>

Java Config:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/view/");
    resolver.setSuffix(".jsp");
    return resolver;
}

This ensures when your controller returns a view name like "dashboard", Spring will look for /WEB-INF/view/dashboard.jsp.

Give these steps a shot, and your JSP pages should load without the 404 error!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:44:55