美文网首页
2 模板引擎freemarker和thymeleaf

2 模板引擎freemarker和thymeleaf

作者: lijiaccy | 来源:发表于2017-06-29 20:36 被阅读0次

freemarker

首先看项目结构


Paste_Image.png

pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<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>

启动类:

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<String,Object> map){
    map.put("name","lijia");
    return "hello";
}

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

hello.ftl

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  Hello World ${name}!!!!
</body>
</html>

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

thymeleaf

项目结构


Paste_Image.png

pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- jdk版本号,angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

启动类:

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<String,Object> map){
    map.put("name","lijia");
    return "hello";
}

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

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello World!</title>
</head>
<body>
     Hello World <p th:text="${name}"></p>
</body>
</html>

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

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


然后才能使用thymeleaf的语法。

相关文章

网友评论

      本文标题:2 模板引擎freemarker和thymeleaf

      本文链接:https://www.haomeiwen.com/subject/ydypcxtx.html