1

I am using Spring Tool Suit and created new Spring Starter Project.

Views are not getting rendered in my spring boot application. I tried to search on various websites but did not get any valid answer which solves the problem.

I don't know the default location of the views but as far as I googled, it should be at resources folder.

Here is the code

The main controller

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {

    @GetMapping("/")
    public String init() {
        return "index";
    }
}

Application.properties

    spring.mvc.view.prefix: /WEB-INF/jsp/
    spring.mvc.view.suffix: .jsp

Application class

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>
    </dependencies>

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


</project>

Project structure

Tushar Girase
  • 183
  • 4
  • 15
  • Are you getting any error or message in the browser or the application logs? – Naros Feb 16 '17 at 03:00
  • There is no error in logs `2017-02-16 08:20:15.844 INFO 11344 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2017-02-16 08:20:15.844 INFO 11344 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2017-02-16 08:20:15.859 INFO 11344 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms` – Tushar Girase Feb 16 '17 at 03:42

4 Answers4

1

if you are using thymeleaf then put your .html files in resources/templates but if you are using jsp views, put .jpa files in src/webapp/WEB-INF/jsp

for jsp dependency------------------------------

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>`

Application.properties

spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp
ovi
  • 30
  • 1
  • 6
  • getting following output Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Feb 16 10:52:16 IST 2017 There was an unexpected error (type=Not Found, status=404). /WEB-INF/jsp/index.jsp – Tushar Girase Feb 16 '17 at 05:22
  • update jasper jar https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper/9.0.0.M15 – ovi Feb 16 '17 at 05:48
0

Spring Boot generally doesn't use WEB-INF. Move your JSP to resources/templates and drop the lines from your application properties.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

you need to compile jsp.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>provided</scope>
</dependency>
Seamas
  • 1,041
  • 7
  • 13
0

Finally I got the answer after two days

  1. Added the dependency as above answers suggested

    <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>

2.Found a solution here

From that answer I have created a source folder src/main/webapp and in this source folder I have added the /WEB-INF/jsp/index

Community
  • 1
  • 1
Tushar Girase
  • 183
  • 4
  • 15