3
votes

I am exploring how JSPs can be served from JAR file. I'm using SpringBoot 1.5.10.RELEASE

I referred this & this examples and was able to create spring boot app which

  • includes JSPs at location src/main/resources/META-INF/resources/WEB-INF/jsp/
  • view resolver has prefix as /WEB-INF/jsp/ and suffix as .jsp

Strange thing is when I ran project as springboot app or Java App, I was able to hit the controller and got desired JSP file as response. After that I thought of packaging the app and then running it from JAR.

$ mvn package
$ java -jar target/springboot-web-jar-demo-0.0.1-SNAPSHOT.jar

Spring boot app comes up, when I hit the same URL pattern, control comes in the mapped method but after that...

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Feb 28 21:01:09 IST 2018
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/index.jsp

My problem/question...

  • Is there any major difference between the springboot app runs in IDE and when it runs as JARs, which can even cause 404?
  • Does spring boot 1.5.10.RELEASE support serving JSPs from JAR? (if yes then what I'm missing here, if no then where it is mentioned) - stackoverflow-link

I have tried searching proper solution or answer to my problem but didn't get any. Hence posting the question here please help.

Note:

  • Please don't provide solution for WAR packaging
  • I know JSP are not best choice nowadays, I'm just learning
  • I'm aware that I can look for alternatives like thymeleaf, freemarker and velocity
  • I don't want to downgrade the SpringBoot version
  • I've also tried specifying <scope> of jasper & JSTL to provided and compile but no luck

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.atuldwivedi.learn.springboot</groupId>
<artifactId>springboot-web-jar-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboot-web-jar-demo</name>
<description>Learn how to serve JSP from JAR</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

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

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

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>
3
Criticisms are welcome, you can down vote the question, but please share your thought why you did so... Thanks! - Atul Dwivedi

3 Answers

0
votes

For you spring boot recognize their jsps, you debt create a webapp directory in src.main like this:

src/main/webapp/WEB-INF/jsp

and not like:

src/main/resources/META-INF/resources/WEB-INF/jsp

0
votes

for dispatcherServelet recognize their jsps, you create a webapp directory in src.main like this:

src/main/webapp/WEB-INF/jsp and also autoconfigure your application in application.properties spring.mvc.view.prefix:/WEB-INF/jsp spring.mvc.view.suffix:.jsp`

-1
votes

JSPs are not supported when using an executable jar.