Spring boot integration freemarker of Spring Cloud 2.x series

Keywords: Java FreeMarker Spring Maven

The page rendering tool in spring boot is thymeleaf. freemarker, a template engine, is also widely used.

1. Spring MVC represents view layer components in spring

2. Why use freemarker: Simple, easy to learn, logical

3. freemarker Advantages: No dependence on servlet s, networks or web environments


1. Create a new maven project. The corresponding pom.xml file is as follows

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>spring-cloud</groupId>

   <artifactId>sc-freemarker</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>

   <name>sc-freemarker</name>

   <url>http://maven.apache.org</url>



   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>

   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>

      </dependencies>

   </dependencyManagement>


   <dependencies>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>


      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-freemarker</artifactId>

      </dependency>

   </dependencies>

</project>

2. New configuration file application.yml

server:
  port: 8081


spring:
  application:
    name: sc-freemarker
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset:UTF-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    suffix: .ftl
    templateEncoding: UTF-8
    templateLoaderPath: classpath:/templates/
    expose-spring-macro-helpers: false

Note: What configurations freemarker has for reference classes

org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties

3. Create a new controller

package sc.freemarker.controller;

import java.util.ArrayList;

import java.util.List;



import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;



import sc.freemarker.model.User;



@Controller

public class FreemarkerController {



   @RequestMapping("/getUser")

   public StringgetUser(Integer id, Model model){

      User u = new User();

      u.setId(1);

      u.setAge(13);

      u.setSex(1);

      u.setUserName("gold");

      User l = new User();

      l.setId(1);

      l.setAge(23);

      l.setSex(0);

      l.setUserName("silver");

      List<User> friends = newArrayList<User>();

      friends.add(l);

      u.setFriends(friends);

      model.addAttribute("user",u);

      return"getUser";

   }



}

Note: Note that the Outermost Control Layer Annotation can only be used with @Controller

4. New template file getUser.ftl

<!DOCTYEhtml>

<htmlxmlns="http://www.w3.org/1999/xhtml"

      xmlns:th="http://www.thymeleaf.org"

      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>

<title>Hello World!</title>

</head>

<body>

      <center>

           <p>welcome${user.userName} to freemarker!</p>

           <p>

                      Age: ${user.age}</br>

                 Gender:

                 <#ifuser.sex==0> female <#elseifuser.sex==1> male <#Else> secrecy

                 </#if>

           </p>

           <h4>My good friend:</h4>

           <#Listuser. friends as f > Name: ${f.userName}, age ${f. age} < br >

           </#list>

      </center>

</body>

</html>

5. Other project documents are as follows


6. Run the freemarker Application. Java class to start the project, and check the startup information to confirm whether the freemarker configuration is in effect.


7. Visit the page http://127.0.0.1:8081/getUser


Source code:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-freemarker

The author of this article: java paradise

This article is from Yunqi Community Partners. JAVA paradise To learn about the relevant information, you can pay attention to " JAVA paradise"

Posted by BDabrowski on Tue, 22 Jan 2019 17:03:13 -0800