问题描述
我有一个使用 Maven 构建的 Spring-MVC 的 web 应用程序.当我生成 JAR 文件时,应用程序启动得很好.控制器正在执行,但是当我到达这一部分时:
I have a webapp using Spring-MVC built with Maven. When I generate the JAR file the app start just fine. The controller is execute but when I reach this part:
@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 中调试或运行可以正常工作.
Note that debugging or running in the IDE works fine.
我的文件夹:
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.
I checked the generated JAR file and I can't see WEB-INF anywhere.
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
文件夹.
You should create a .war
rather than a .jar
for a web application and you will see the WEB-INF
folder.
也变了
spring.mvc.view.prefix=/WEB-INF/
至spring.mvc.view.prefix=/WEB-INF/templates
和
ModelAndView("templates/test-block")
到 ModelAndView("test-block")
以解决 404 错误.
ModelAndView("templates/test-block")
to ModelAndView("test-block")
to address the 404 error.
这篇关于WEB-INF 未包含在使用 SpringBoot、Spring-MVC 和 Maven 的 WebApp 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!