首页 > 解决方案 > WEB-INF 未添加到 SpringBoot jar

问题描述

我正在使用 SpringBoot 组合一个 webapp。

REST 部分工作正常,但“JSP 部分”很糟糕。端点被调用,一切都很好,直到我返回 JSP 页面的基本名称,例如return "info";The method returns a String.

当我调用端点时,我收到以下消息:

[...] [dispatcherServlet]:Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [Could not resolve view with name 'info' in servlet with name 'dispatcherServlet'],根本原因是 javax。 servlet.ServletException:无法解析名称为“dispatcherServlet”的 servlet 中名称为“info”的视图

我遵循“模式”为 SpringBoot 添加 JSP 支持(我说“模式”是因为我发现的十几个来源似乎都引用了相同的示例)。

我正在用 Maven 构建一个 JAR,查看那个 jar,我没有找到 JSP 文件,甚至没有任何 WEB-INF 目录结构,所以我相信问题出在 Maven 和 SpringBoot 的插件之间。

以下是 POM 的片段,删除了大部分依赖项:

<packaging>jar</packaging>
...

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
...
<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

POM 主要是根据原型构建的。

我对调试 Maven 构建相当无知,假设这是一个 Maven 问题,但是,我想它可能与 springboot 插件有关。我感谢所有帮助,包括比我找到的更好的阅读资源。

更新 Per Qiu Zhou 关于 的评论,我在 POM 中添加了以下内容:

    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

并将 JSP 目录移动到 src/main/resources/WEB-INF/jsp。JAR 文件现在包含以下内容:

BOOT-INF/classes/WEB-INF/jsp/info.jsp

这似乎是正确的。我不再收到 dispatcherServlet 消息(见上文),但是,当我 curl 站点时,我得到的是:

% curl localhost:8099/info
{"timestamp":"2020-09-01T17:22:28.413+00:00","status":404,"error":"Not Found","message":"","path":"/info"}

控制器代码是这样的:

@Controller
public class SimpleController {
   @GetMapping("/info")
   public String info() {
      return "info";
  }
}

我仍然感到困惑——JAR 是否正确构建?谢谢

标签: springspring-bootmaven

解决方案


您没有指定要使用的插件版本。根据版本,您可能需要配置插件以将资源目录添加到应用程序类路径,方法是将以下内容添加到插件配置中

<configuration>
    <addResources>true</addResources>
</configuration>

这启用了资源的热重载,但现在为此和其他开发功能实际推荐的是添加 spring 开发工具依赖项。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>2.3.3.RELEASE</version>
        <optional>true</optional>
    </dependency>
</dependencies>

您可以通过阅读插件文档了解更多信息。 https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/#run


推荐阅读