3

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

2
  • /webapp/ will not load the template from /src/main/webapp but from /src/main/resources/webapp`... Commented Nov 23, 2016 at 15:15
  • Instead pasting solution in question, you should accept the answer. Do not post answers in question. Commented Jun 26, 2018 at 9:35

5 Answers 5

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ! I was putting my html in wepapp/templates and it's resources/templates ! Thanks a lot.
thank you. i didn't know '@ResponseBody ' will be serialized to JSON.
Thank you. I was using @ RestController instead of @ Controller.
@bakouz Answers need to be marked as accepted if it resolves your query. This will help other people
7

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.

3 Comments

Index.html in wepapp with what's you write, give me an error: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
I was expecting this error . Did you import the thymeleaf dependency ? Rendering html content only works with thymeaf. Your webapp is a RESP app.
I put this in my pom. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> and this in my index.html <html xmlns:th="thymeleaf.org">
0

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

2 Comments

It's ok for index.html in static folder. But i have same probleme with another view.. I'll update my post
Thx for Tymeleaf. But I have another one error now. I updated my post
0

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.