I am working on a web application, which meant to be extendable in a way that jars should contain everything that is required for the extension or module, including models, views as well as controllers. One jar is for a single extension. I am however stuck with referring to JSPs inside a JAR, though after a day trying and googling I am no longer sure if it is possible at all.
Environment and libraries, in case they are relevant: STS 4, Tomcat 9, Java 8, Spring 5.2.2.RELEASE, servlet-api 4.0.1
The configuration of the framework is XML based and the extensions are annotation based.
What I have:
mvc-dispatcher-servlet.xml (goes into WAR)
<mvc:annotation-driven />
<!-- it instantiates BarController from the JAR that meant to use bar.jsp from the JAR -->
<context:component-scan base-package="acme.jar.with.jsps.controllers.are.in.this.pkg" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- it instantiates FooController that uses foo.jsp -->
<bean class="acme.this.controller.is.not.in.jar.FooController" />
bar.jsp is in the jar
pom.xml
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
...
and bar.jsp
$ find . -name "*.jsp"
./target/classes/WEB-INF/views/jsp/bar.jsp
./src/main/webapp/WEB-INF/views/jsp/bar.jsp
foo.jsp from the WAR is all good
DEBUG|org.springframework.web.servlet.view.JstlView|View name 'FooController', model {...
DEBUG|org.springframework.web.servlet.view.JstlView|Forwarding to [/WEB-INF/views/jsp/foo.jsp]
DEBUG|org.springframework.web.servlet.DispatcherServlet|Completed 200 OK
bar.jsp from the JAR cannot be found
DEBUG|org.springframework.web.servlet.view.JstlView|View name 'BarController', model {...
DEBUG|org.springframework.web.servlet.view.JstlView|Forwarding to [/WEB-INF/views/jsp/bar.jsp]
DEBUG|org.springframework.web.servlet.DispatcherServlet|Completed 404 NOT_FOUND
My questions
- Can a JSP in JAR be referred at all? I tried to use many different prefixes for
InternalResourceViewResolver
but neither of them was able to get the JSP from JAR. - Is there a view resolver that is capable of extracting the JSPs from JARs? If so, then how can multiple view resolvers be configured?
- Am I doing something wrong?