Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 282
crmepham Avatar asked Feb 19 '17 18:02

crmepham


3 Answers

Spring Boot recently yet again introduced a breaking change, which leads to the infamous whitelabel error page. They changed the default suffix from .ftl to .ftlh.

https://github.com/spring-projects/spring-boot/issues/15131

So for Spring Boot 2.2.x applications, these extensions have to be updated.

like image 192
Jan Bodnar Avatar answered Nov 20 '22 08:11

Jan Bodnar


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

like image 5
rdlopes Avatar answered Nov 20 '22 06:11

rdlopes


Your freemarker template directory settings inside application.properties is wrong. It should be:

spring.freemarker.template-loader-path=classpath:/templates/
like image 3
Monzurul Shimul Avatar answered Nov 20 '22 07:11

Monzurul Shimul