【发布时间】:2019-03-11 05:58:32
【问题描述】:
我有一个使用 Maven 构建的 Spring-MVC 的 web 应用程序。当我生成 JAR 文件时,应用程序启动得很好。控制器正在执行,但是当我到达这部分时:
@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>
【问题讨论】:
-
你不应该生成一个 .war 文件吗?
-
我想要一个独立的 jar 文件。但尝试做一个战争文件,现在该文件夹已包含在内。奇怪
-
使用返回简单字符串值(视图名称)的请求映射器或在
ModelAndView对象的构造函数中设置模型名称和值。 -
单个 jar 文件不是独立的 Web 应用程序。只是一个模块,如果您将它们包含在打包到 WAR/EAR 文件中的 Web 应用程序的类路径中,您可以在 Web 应用程序中重用它。
-
@TheBitman 是的,Spring Boot 应用程序通常捆绑为单个 jar 文件,其中包含嵌入式 Web 服务器。
标签: java maven spring-mvc spring-boot