bakouz
bakouz

Reputation: 64

Spring Boot MVC, not returning my view

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 :)

Upvotes: 3

Views: 18381

Answers (5)

Calis18
Calis18

Reputation: 47

For me, I've just replaced this dependency :

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
</dependency>

By this one :

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

Upvotes: 0

bakouz
bakouz

Reputation: 64

Here is the full answer to return a view with Spring boot mvc. Thanks to all.

Controller

@Controller
@RequestMapping("/map")
public class MapController {

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

src/main/resources/templates/map.html

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

    <div id="main">
        <div class="row">
            <p>my map</p>
        </div>
    </div>
</body>
</html>

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>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>



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

Upvotes: 0

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44705

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.

Upvotes: 10

mohan rathour
mohan rathour

Reputation: 430

Static resources, like HTML or JavaScript or CSS, can easily be served from your Spring Boot application just be dropping them into the right place in the source code. By default Spring Boot serves static content from resources in the classpath at "/static" (or "/public"). The index.html resource is special because it is used as a "welcome page" if it exists, which means it will be served up as the root resource.Add the maven dependency Thymeleaf

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

So create this file:

src/main/resources/static/index.html

at the right place and you can put other static files into you webapp folder

Upvotes: 0

AchillesVan
AchillesVan

Reputation: 4356

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.

Upvotes: 7

Related Questions