我有一个使用 Spring-MVC 和 Maven 构建的 Web 应用程序。当我生成 JAR 文件时,应用程序启动正常。 Controller 正在执行,但是当我到达这一部分时:
@RequestMapping(value = "/test-block", method = RequestMethod.GET)
public ModelAndView block(Model model) {
return new ModelAndView("templates/test-block");
}
我收到这个错误:
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/templates/test-block.jsp
请注意,在 IDE 中调试或运行都可以正常工作。
我的文件夹:
src
|--main
|--java
|--com.xxx // sources
webapp
|--WEB-INF
|--templates
|--*.jsp // jsp files
resources
|--application.properties
我的应用程序属性:
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
我检查了生成的 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>
最佳答案
您应该为 Web 应用程序创建 .war
而不是 .jar
,您将看到 WEB-INF
文件夹。
也改
spring.mvc.view.prefix=/WEB-INF/
到
spring.mvc.view.prefix=/WEB-INF/templates
和
ModelAndView("templates/test-block")
到 ModelAndView("test-block")
以解决 404 错误。
关于java - 使用 SpringBoot、Spring-MVC 和 Maven 的 WebApp 中不包含 WEB-INF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52670491/