0
votes

I'm developing simple Spring Boot MVC application. Althought I have read many answers on SO I still can't fix Spring Boot not finding my .jsp files - it displays whitelabel error page.

Project Structure: Project structure.

  1. pom.xml

        <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.learnbook</groupId>
            <artifactId>learnbook</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>war</packaging>
            <name>LearnBook</name>
            <description>We want to make the best courses in the market </description>
    
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.5.3.RELEASE</version>
            </parent>
    
        <dependencies>
    
                <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                    <version>1.2</version>
                </dependency>
    
                <dependency>
                    <groupId>org.hibernate.common</groupId>
                    <artifactId>hibernate-commons-annotations</artifactId>
                    <version>4.0.4.Final</version>
                </dependency>
    
                <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <version>4.3.10.Final</version>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-security</artifactId>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
    
                <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>4.3.11.Final</version>
                </dependency>
    
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <scope>provided</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-taglibs</artifactId>
                </dependency>
    
                <dependency>
                    <groupId>commons-fileupload</groupId>
                    <artifactId>commons-fileupload</artifactId>
                    <version>1.3.1</version>    
                </dependency>
    
                <!-- Apache Commons Upload -->
                <dependency>
                    <groupId>commons-io</groupId>
                    <artifactId>commons-io</artifactId>
                    <version>2.4</version>
                </dependency>
    
                 <dependency>
                    <groupId>com.cloudinary</groupId>
                    <artifactId>cloudinary-http44</artifactId>
                    <version>1.4.6</version>
                </dependency>
    
                <dependency>
                    <groupId>com.cloudinary</groupId>
                    <artifactId>cloudinary-taglib</artifactId>
                    <version>1.4.6</version>
                </dependency>
    
            </dependencies>
    
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
    
    
            <properties>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.source>1.8</maven.compiler.source>
                <java.version>1.8</java.version>
            </properties>
        </project>
    

2.LearnBookApplication.java

    @EnableJpaRepositories(basePackages = "net.learnbook.repository")
    @SpringBootApplication
    public class LearnBookApplication {

        public static void main(String[] args) {
            SpringApplication.run(LearnBookApplication.class, args);
        }

        @Bean
        @ConfigurationProperties(prefix = "datasource.main")
        public DataSource siteDataSourceBean() {
            return DataSourceBuilder.create().build();
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean siteEntityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
            return builder.dataSource(siteDataSourceBean()).packages("net.learnbook.model").persistenceUnit("learnbookPU")
                    .build();
        }

    }

3.ServletInitializer.java

    public class ServletInitializer extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(LearnBookApplication.class);
        }
    }
  1. web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
      <display-name>learnbook</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    
  2. application.properties

    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    security.basic.enabled=false
    
    datasource.main.url: jdbc:mysql://localhost/learnbook?autoReconnect=true&useSSL=false
    datasource.main.username:root
    datasource.main.password:root
    datasource.main.driverClassName:com.mysql.jdbc.Driver
    
    spring.jpa.generate-ddl=true
    spring.jpa.hibernate.ddl-auto:create-drop
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.show-sql=true
    
  3. Controller

    @Controller
    public class ViewController {
    
        @RequestMapping(value = "/", method = { RequestMethod.GET,RequestMethod.POST}, name = "home_index")
        public String index() {
            return "index";
        }
    
    }'
    
1
suppose you follow this example. - Rajith Pemabandu

1 Answers

0
votes

FYI

From Spring boot Documentation:

JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers. You can take further reading here:

http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines

I believe that this means if you still want to use JSP pages, then you'll have to get rid of the embedded tomcat.