Spring Boot MVC, not returning my view

15,040

Solution 1

There are several things to look at:

Make sure you're using @Controller in stead of @RestController.

If you're using @RestController, the return value of your method will be serialized to JSON and returned, rather than being resolved to a view.

Include a template library

If you're returning a view, you probably need a template library such as Thymeleaf, Velocity, ... . If you want to use Thymeleaf you can use:

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

Make sure your template is in the right spot

The default location for adding templates is src/main/resources/templates, make sure your templates are located here. The locations src/main/resources/public and src/main/resources/static are used for serving static content, but in that case, you don't have to provide a controller and you can just go to: http://localhost:8080/map.html.

Solution 2

The page with "index" written and not your index.html is normal since you are returning your content in Json format thanks to @RestController. Do this instead :

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//@RestController
@Controller
@RequestMapping("/")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "index";
    }
}

Make sure you imported the Thymeleaf dependency in your maven.

Share:
15,040
bakouz
Author by

bakouz

Updated on June 15, 2022

Comments

  • bakouz
    bakouz almost 2 years

    I have problem with Spring Boot MVC and views. When I go on localhost:9090, I've got page with "index" written and not my index.html page like view. Can you tell my what the problem is ? Thanks in advance.

    pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    
    <properties>
        <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-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    MainController

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/")
    public class MainController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String index() {
            return "index";
        }
    }
    

    Application.yml src/main/resources/application.yml

    spring:
        data:
            rest:
                basePath: /api
        mvc:
            view:
                prefix: /webapp/
                suffix: .html
    
    server:
        port: 9000
    

    My index.html is in : src/main/webapp/index.html

    Maybe useless but : Application.java

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

    UPDATE: I put index.html in src/static/index.html Ok but I have same problem with :

    I add in my pom:

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

    In map.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    
    <body>
     <p>this is the map</p>
    </body>
    

    MapController

    @Controller
    @RequestMapping("/map")
    public class MapController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String index() {
            return "map";
        }
    }
    

    Map.html is in wepapp. When I go to localhost:9090/map, I've the following error on chrome :

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

    And in eclipse I've got this error :

    Error resolving template "map", template might not exist or might not be accessible by any of the configured Template Resolvers
    

    Map maybe should be in webapps/templates? I really dont know.

    SOLUTION : Thanks guys ! Controller

    @Controller
    @RequestMapping("/map")
    public class MapController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String index() {
            return "map";
        }
    }
    

    And the views must be in src/main/resources/templates/map.html and NOT in wepapp/templates/map.html map.html

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Bienvenue sur le portail historique intéractif - Amysto</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    
        <div id="main">
            <div class="row">
                <p>Il faut mettre la map ici</p>
            </div>
        </div>
    </body>
    
    <script src="./foundation/js/vendor/jquery.js"></script>
    <script src="./js/menu.js"></script>
    
    
    </html>
    

    I can't upvote so I let you do guys :)