javaspringspring-mvcspring-bootfreemarker

Spring Boot unable to resolve Freemarker view


I am attempting to get Freemarker views displaying with Spring Boot for the first time, and currently get the following error in the browser:

Whitelabel Error Page

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

Sun Feb 19 17:59:07 GMT 2017 There was an unexpected error (type=Not Found, status=404). No message available

I am using Spring Boot 1.5.

My file structure:

enter image description here

LoginController

package com.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crm.service.LoginService;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/login")
    public String authenticate() {
        return "loginPage";
    }
}

application.properties

spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl

Server

package com.crm;

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

@SpringBootApplication
public class Server{

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

Why is Spring unable to resolve the loginPage.ftl view? Why can't I see it in the web browser?


Solution

  • With Spring Boot 1.5.1, you don't need to add those properties to your application.properties file, they're already defined, thanks to autoconfiguration.

    Remove those 2 properties and it should work with the following dependencies in your pom.xml:

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

    Documentation can be found here: http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines