Spring Boot integrates static technology FreeMarker

Original link: http://www.yiidian.com/springboot/springboot-freemarker.html

This article explains how to integrate FreeMarker in Spring Boot.

1 create project, import dependency

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <!-- Import springboot Parent project. Note: any SpringBoot Project must have!!! -->
    <!-- The function of parent project: lock the version number of the starting dependency, but not the dependency -->

        <!--web Start dependence-->

        <!-- freemarker -->

Remember to rely on FreeMarker here, otherwise it can't run!

2 write Controller

package com.yiidian.controller;
import java.util.ArrayList;
import java.util.List;

import com.yiidian.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

 * Controller
 * A little tutorial - www.yidian.com
public class UserController {

	 * User list display
	public String list(Model model){
		//Analog user data
		List<User> list = new ArrayList<User>();
		list.add(new User(1,"Xiao Zhang",18));
		list.add(new User(2,"petty thief",20));
		list.add(new User(3,"Xiao Chen",22));

		//Store data in model
		model.addAttribute("list", list);

		//Go to freemarker page: list.ftl
		return "list";

When Controller transfers data to FreeMarker template for rendering

3. Prepare FreeMarker template file

FreeMarker template files must be placed in the directory / resources/templates with the suffix of. ftl. The contents are as follows:

	<title>A little tutorial network-User list display</title>
	<meta charset="utf-8"/>
		<h3>User list display</h3>
		<table border="1">
				<th>Full name</th>
			<#list list as user>

4 running tests


Source code download: https://pan.baidu.com/s/10WaOAfrWHG-v2M8QFe2zMA

Welcome to my official account: a little tutorial. Get exclusive learning resources and daily dry goods push.
If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Keywords: Java FreeMarker SpringBoot Spring Maven

Added by Ceril on Tue, 24 Mar 2020 16:37:54 +0200