WEB-INF 未包含在使用 SpringBoot、Spring-MVC 和 Maven 的 WebApp 中

Mar*_*o L 7 java spring-mvc maven spring-boot

我有一个使用 Maven 构建的 Spring-MVC 的 web 应用程序。当我生成 JAR 文件时,应用程序启动得很好。控制器正在执行,但是当我到达这一部分时:

@RequestMapping(value = "/test-block", method = RequestMethod.GET)
public ModelAndView block(Model model) {
    return new ModelAndView("templates/test-block");
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

There was an unexpected error (type=Not Found, status=404).
/WEB-INF/templates/test-block.jsp
Run Code Online (Sandbox Code Playgroud)

请注意,在 IDE 中调试或运行可以正常工作。

我的文件夹:

src
|--main
   |--java
      |--com.xxx // sources
      webapp
      |--WEB-INF
         |--templates
            |--*.jsp // jsp files
      resources
      |--application.properties
Run Code Online (Sandbox Code Playgroud)

我的应用程序属性:

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
Run Code Online (Sandbox Code Playgroud)

我检查了生成的 JAR 文件,但在任何地方都看不到 WEB-INF。

编辑:

pom文件:

    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

Nic*_*s K 5

您应该为 Web 应用程序创建 a.war而不是 a .jar,您将看到该WEB-INF文件夹。

也变

spring.mvc.view.prefix=/WEB-INF/ 到 spring.mvc.view.prefix=/WEB-INF/templates 和

ModelAndView("templates/test-block")来ModelAndView("test-block")解决 404 错误。

  • modelAndView 实际上正在工作。我收到 404 错误是因为 JAR 中未包含 WEB-INF,因此缺少 test-block.jsp。创建了 WAR,现在所有文件都包含在内。谢谢! (2认同)