ben
ben

Reputation: 142

Spring MVC with jsp files in WAR as well as in JAR files

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

Upvotes: 1

Views: 851

Answers (1)

Alex Chernyshev
Alex Chernyshev

Reputation: 1745

Well, the whole story of 'JSP inside JAR' is long and full of pain.

If in short: even if you would be able to make it work - there always will be some broken parts.

Here is more-less working sample: https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

Here is similar question with some details:
Is it possible with Spring Boot to serve up JSPs with a JAR packaging?

Best case will be to use WAR packaging instead of JAR.

Upvotes: 1

Related Questions