0

Unfortunately haven't found an answer in official documentation. Maybe what I'm trying to do is not supported event by Tomcat, but still. Is it possible to make spring-boot/Tomcat to resolve JSP pages from .jar file that is in the classpath?

I have a spring-boot (2) application that is packed as a war file. There are a numerous jsp pages in 'webapp/view' folder, and appropriate MVC configuration:

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

All these pages are being resolved. Okay.

But. The project is a multi-module maven one. It supports builds with different dependencies (my own modules) depending on maven profiles.

I need to make application to resolve JSPs from those optional dependencies that are included into runtime as jars in a classpath.

And I'm getting Whitelabel error that says that JSP files can not be found.

Is it even possible to make it work? And if it is, than how?

P.S.: I have already tried to make some magic with copying JSPs into "root" spring-boot application itself and it works, but this way is dirty and tricky.

Igor Petrov
  • 166
  • 2
  • 15
  • Can you post your `pom.xml` ? – Avijit Barua Apr 22 '19 at 07:16
  • @Avijit Barua, I've posted maven-dependency-plugin configuration below. But the final solution is more accurate. – Igor Petrov Apr 22 '19 at 10:17
  • 1
    https://stackoverflow.com/questions/53872767/im-getting-error-404-while-trying-to-access-my-spring-boot-app-on-amazon-elasti/53921410#53921410 please check this answer – Dipak Thoke Apr 22 '19 at 11:01
  • Well, it's close to my question, but it's not exactly what I asked about. It "reveals" how to resolve JSPs in executable jar. I use a word reveal here because this topic is not covered well in documentation. But in my case the problem is in resolving JSPs from jars that are included into war as a dependency. Nevertheless, thanks for the reference – Igor Petrov Apr 22 '19 at 12:54
  • Possible duplicate of [I'm getting error 404 while trying to access my spring boot app on Amazon Elastic Bean Stalk](https://stackoverflow.com/questions/53872767/im-getting-error-404-while-trying-to-access-my-spring-boot-app-on-amazon-elasti) – recnac Apr 28 '19 at 01:20

1 Answers1

0

I don't think it is worth to publish the whole pom.xml, but here is the maven-dependency-plugin section that works in my case:

            <!-- ... -->
            <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>
            <!-- ... -->

The only problem here is that it works only when you launch the executable .war. Launching this application from IDE (IntelliJ IDEA) need some additional steps to be made. And that's another one disadvantage of this solution, to my mind. The first one is a dirty pom.xml.

TL;DR

Solution:

BUT!. I have found another solution that is more suitable, I think. Actually I have moved from Tomcat to Jetty, and solution above works fine even there. There's no need in hacking the build anymore.

Now I put my .jsp files into src/main/webapp/META-INF/resources/view/some_module folder in some_module dependency and resolve it by path 'some_module/someJsp' via standard Spring's @Controllers.

I apologize that I haven't found this topic earlier. Now this is a duplicate. But who knows, maybe someone will apply solution with maven dependency plugin.

Igor Petrov
  • 166
  • 2
  • 15