2 模板引擎freemarker和thymeleaf

freemarker

首先看项目结构


2 模板引擎freemarker和thymeleaf_第1张图片
Paste_Image.png

pom.xml:


    UTF-8
    
    1.8



    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE



    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-freemarker
    

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;

@Controller
@SpringBootApplication
public class Main {
@RequestMapping("/")
public String hello(Map map){
    map.put("name","lijia");
    return "hello";
}

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

hello.ftl



    
    Title


  Hello World ${name}!!!!


运行启动类,在浏览器中输入:
http://localhost:8080/

2 模板引擎freemarker和thymeleaf_第2张图片

thymeleaf

项目结构


2 模板引擎freemarker和thymeleaf_第3张图片
Paste_Image.png

pom.xml:


    UTF-8
    
    1.8



    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE



    
        org.springframework.boot
        spring-boot-starter-web
    
   
        org.springframework.boot
        spring-boot-starter-thymeleaf
    

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;

@Controller
@SpringBootApplication
public class Main {
@RequestMapping("/")
public String hello(Map map){
    map.put("name","lijia");
    return "hello";
}

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

index.html




    Hello World!


     Hello World 

运行启动类,在浏览器中输入:
http://localhost:8080/

2 模板引擎freemarker和thymeleaf_第4张图片

在thymeleaf中,首先加入下面这句话


然后才能使用thymeleaf的语法。

你可能感兴趣的:(2 模板引擎freemarker和thymeleaf)