Spring boot provide view technology to front development. here is spring boot freemarker example. spring-boot-starter-freemarker dependency requires in class path so spring boot automatically configure freemarker configuration.   For freemarker require .ftl template file which must be available inside resources\templates folder.

 

spring boot freemarker example

spring boot freemarker example

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-freemarker-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <description>spring boot freemarker example</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- Provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>                <!-- for tomcat web container-->
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>                 <!--  its requires for spring boot freemarker  -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

FreeMarkerController

Its like normal spring controller which handle require and return String which is name of freemarker template name which is available in side resources\templates. Spring will try to find resources\templates\[TEMPLATE_NAME].ftl file.

package com.javadeveloperzone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@Controller
public class FreeMarkerController {
    @GetMapping("/welcome")                     // it will handle all request for /welcome
    public String welcome(ModelMap madelMap) {
        java.util.List<Employee> employeeList=new java.util.ArrayList<>();
        employeeList.add(new Employee(1,"Jone"));
        employeeList.add(new Employee(2,"Moke"));
        employeeList.add(new Employee(3,"Rijos"));
        madelMap.put("users",employeeList);
        return "welcome";           // welcome is view name. It will call welcome.jsp
    }
}

SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@SpringBootApplication
@ComponentScan
// Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootConfig.class);
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

Employee

package com.javadeveloperzone.controller;
/**
 * Created by JavaDeveloperZone on 26-09-2017.
 */
public class Employee {
    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
    private int id;
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}

welcome.ftl

It must be available insider resources\templates folder.

<html>
<head>
    <title>spring boot freemarker example</title>
</head>
<body>
<h1>Here is list of employee using spring boot freemarker</h1>
<table>
<#list users as user>
    <tr>
        <td>${user.id}</td>
        <td>${user.name}</td>
    </tr>
</#list>
</table>
</body>
</html>

Demo

spring boot freemarker example demo

spring boot freemarker example demo

References:

freemarker document

Spring boot freemarker document

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *