PepeRaikkonen
PepeRaikkonen

Reputation: 39

JSP Spring Boot works when running from IntelliJ, but empty page when running WAR

I have an issue with my spring boot app: it works fine when running form IntelliJ or with mvn clean spring-boot:run. However when I package it with man package it serves an empty page ignoring my jsps.

I'm packaging with war. I plan to run the project with embedded tomcat

my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <packaging>war</packaging>
    <groupId>com.some</groupId>
    <artifactId>letdeskWebsite</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>some Website</name>
    <description>some Website</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jsp-api</artifactId>
                    <groupId>javax.servlet.jsp</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>target</warSourceDirectory>
                    <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

My main class:

@SpringBootApplication
public class SomeWebsiteApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SomeWebsiteApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(SomeWebsiteApplication.class, args);
    }

}

controller:

    @GetMapping({"/", "/hello"})
    public String hello(final Model model,
                        @RequestParam(value="name",
                                required=false,
                                defaultValue="World") final String name) {
        model.addAttribute("name", name);
        log.info("called a home page");
        return "index";
    }
}

my jsp (located at src/main/webapp/WEB-INF/jsp/index.jsp):

<!DOCTYPE html>
<html class="no-js" lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Hello ${name}!</title>
        <meta class="foundation-mq">
        <link rel="stylesheet" href="css/app.css">
    </head>
    <body>
        <div class="pageWithoutFooter">
            <jsp:include page="NavigationElements/topNavigation.jsp"/>
            <h2 class="hello-title">Hello ${name}!</h2>
            <input type="button" value="Hi there!">
        </div>
        <jsp:include page="NavigationElements/footerNavigation.jsp"/>
    </body>
</html>

webconfig:

@ComponentScan
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");

        return resolver;
    }

I had spent hours trying to find the solution but for no avail

Upvotes: 2

Views: 411

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44960

Spring Boot is a convention over configuration framework and you are not following the usual conventions now. If you plan to run with embedded Tomcat you should use spring-boot:repackage mojo instead of maven-war-plugin. This also means that spring-boot-starter-tomcat has to be compile and you don't need quite a few things from your pom.xml.

Your current setup is overcomplicated because you are building a WAR that should be deployed into an existing Tomcat. You might want to start fresh with something simpler.

  1. Use https://start.spring.io/ to generate a basic Maven project with Spring Web dependency.
  2. Move your current code and include the JSPs
  3. Package as running JAR with embedded Tomcat
  4. Check that JSPs are served correctly

Upvotes: 1

Related Questions