【问题标题】:Spring boot: resolve JSPs from jar dependenciesSpring boot:从 jar 依赖项中解析 JSP
【发布时间】:2019-09-11 09:55:18
【问题描述】:

很遗憾,在官方文档中没有找到答案。也许我想做的不是Tomcat支持的事件,但仍然如此。 是否可以让 spring-boot/Tomcat 从类路径中的.jar 文件中解析 JSP 页面?

我有一个打包为war 文件的spring-boot (2) 应用程序。 'webapp/view' 文件夹中有许多 jsp 页面,以及适当的 MVC 配置:

@Configuration
public class MVCConf implements WebMvcConfigurer {
    // ...
    @Bean
    public ViewResolver internalResourceViewResolver() {
        return new InternalResourceViewResolver(){{
            setPrefix("/view/");
            setSuffix(".jsp");
            setRedirectHttp10Compatible(false);
        }};
    }
    // ...
}

正在解决所有这些页面。好的。

但是。该项目是一个多模块的 Maven 项目。它根据 Maven 配置文件支持具有不同依赖项(我自己的模块)的构建。

我需要让应用程序从那些作为类路径中的jars 包含在运行时中的可选依赖项中解析JSP。

我收到 Whitelabel 错误,提示找不到 JSP 文件。

甚至有可能让它工作吗?如果是,那又如何?

P.S.:我已经尝试通过将 JSP 复制到“根”spring-boot 应用程序本身来创造一些魔法并且它可以工作,但是这种方式很脏而且很棘手。

【问题讨论】:

标签: spring-boot jsp


【解决方案1】:

我认为发布整个 pom.xml 不值得,但这里是适用于我的情况的 maven-dependency-plugin 部分:

            <!-- ... -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>

                            <!-- this skip property is set by maven profile in same pom.xml - no magic here -->
                            <skip>${exclude.some_submodule}</skip>

                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>some_submodule</artifactId>
                                    <version>${project.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.outputDirectory}</outputDirectory>

                                    <!-- there are webapp/view/*.jsp files in some_module's structure -->
                                    <includes>view/**</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- ... -->

这里唯一的问题是它仅在您启动可执行文件.war 时才有效。从 IDE (IntelliJ IDEA) 启动这个应用程序需要一些额外的步骤。在我看来,这是该解决方案的另一个缺点。第一个是脏pom.xml

TL;DR

解决方案:

但是!I have found another solution that is more suitable, I think。 实际上我已经从 Tomcat 搬到了 Jetty,上面的解决方案即使在那里也能正常工作。不再需要破解构建。

现在我将我的 .jsp 文件放入 some_module 依赖项中的 src/main/webapp/META-INF/resources/view/some_module 文件夹中,并通过标准 Spring 的 @Controllers 通过路径 'some_module/someJsp' 解决它。

很抱歉我之前没有找到这个话题。现在这是重复的。但谁知道呢,也许有人会用 maven 依赖插件来应用解决方案。

【讨论】:

    猜你喜欢
    • 2019-11-21
    • 2020-07-08
    • 2019-07-26
    • 2017-06-18
    • 2018-08-11
    • 2019-01-27
    • 2018-05-13
    • 2020-02-28
    • 2019-08-14
    相关资源
    最近更新 更多