5

The problem:

Using Spring 4, I am getting this when visiting a webpage

Whitelabel Error Page

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

Fri Aug 15 16:41:29 BST 2014
There was an unexpected error (type=Not Found, status=404).

What I have:

I have this Main class:

// src/main/java/abc/Main.java
package abc;

import abc.web.WebAppConfig;
import org.springframework.boot.SpringApplication;

public class Main {
    public static void main(String[] args) {
        SpringApplication.run(WebAppConfig.class);

    }
}

Then I have this WebAppConfig.class (currently with just some configuration annotations):

// src/main/java/abc/web/WebAppConfig.java
package abc.web;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class WebAppConfig {

}

And this controller HomeController.java:

// src/main/java/abc/web/HomeController.java
package abc.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@RequestMapping("/")
public class HomeController {

    @RequestMapping(method = GET)
    public String home() {
        System.out.println("HELLO !!");
        return "home";
    }
}

The HELLO !! shows up in the logs.

And finally I have a html file at src/main/java/abc/webapp/home.html, with just some html tags including a p tag with Hello, world!.

The question:

I understand that I am missing the way of rendering the view, but I searched a couple of questions on stackoverflow and haven't find a solution yet.

Can someone explain how can I get Spring to render a webpage ? What am I missing ?

Thanks in advance :)

1
  • 1
    Please post the server logs from the request. Commented Aug 15, 2014 at 18:47

3 Answers 3

11

Spring Boot will automatically use and configure Thymeleaf as the view rendering engine, as long as it's on the classpath. To put it on the classpath use

compile("org.springframework.boot:spring-boot-starter-thymeleaf")

in the gradle build file.

If you are using maven add the dependency:

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

In your case in order to display the home.html view (in accordance to the controller you are using), you need to place it under /resources/templates.

For a complete example, check out this guide.

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

3 Comments

Can you please tell what should I do for maven explicitly?
Use <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
@TejeshRaut Alternatively, you can go to start.spring.io and download a generated package to see what's needed in the maven file.
7

The simplest answer to the "how can I get Spring Boot to render a webpage?" question is: place your home.html file inside src/main/resources/static/ folder. The page will be available under the /home.html URL.

More details in the documentation.

1 Comment

When to put the HTML files under subdirectory "static" and when under "templates"? Thanks
0

You probably not configured template engine and view resolver. See example with thymeleaf here http://www.thymeleaf.org/doc/thymeleafspring.html As thymeleaf template is a valid HTML code and vice verse you can use it.

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.