springboot模板(thymeleaf和freemarker)

本文介绍了Thymeleaf和Freemarker作为SpringBoot的模板引擎,Thymeleaf以其自然模板特性方便前端开发,支持与Spring Framework的集成,而Freemarker则是一款通用模板引擎,通过模板和数据模型生成输出文本。文章详细讲解了两者的基本用法、配置及页面效果。
摘要由CSDN通过智能技术生成

Thymeleaf模板

Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎。

Thymeleaf的主要目标是为 您的开发工作流程带来优雅的 自然模板 -HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。

Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择.

thymeleaf使用th属性赋予每个标签与后台交互的能力,当html文件在本地直接用浏览器打开,浏览器引擎会忽略掉th属性,并正常渲染页面,当把html文件放到服务容器访问,th属性与后台交互,获取数据替换原先的内容,这样前端工程师在编写html页面时,在本地开发,正常实现页面逻辑效果即可,数据先写死,当放到服务容器时数据从后台获取。

相关pom依赖

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

Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行

server:
  port: 80
  servlet:
    context-path:

spring:
  thymeleaf:
    cache: false

正式环境还是要将缓存开启的
User

package com.lx.springboot01.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author润红的爸爸
 * @site www.xiaomage.com
 * @company 潇洒公司
 * @create 2019-11-07 18:10
 */
@ConfigurationProperties(
        prefix = "user"
)
@Component
@Data
public class User {
   
    private String uname;
    private String pwd;
    private Integer age;

    public User() {
   
    }

    public User(String uname, String pwd, Integer age) {
   
        this.uname = uname;
        this.pwd = pwd;
        this.age = age;
    }
}

ThymeleafController

package com.lx.springboot01.controller;

import com.lx.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author润红的爸爸
 * @site www.xiaomage.com
 * @company 潇洒公司
 * @create 2019-11-08 10:47
 */
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
   
    @RequestMapping("/list")
    public ModelAndView list(){
   
        ModelAndView mv = new ModelAndView();
        mv.addObject("name","zs");
        List list = new ArrayList();
        list
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值