SpringBoot模板引擎

本文介绍了SpringBoot中如何集成和使用Thymeleaf与Freemarker模板引擎,包括Thymeleaf的${}语法和list遍历,以及Freemarker的条件语法和list循环。同时,文章详细讨论了在实际应用中遇到的HTML转译问题、实体类get.set缺失错误以及FTL文件路径问题,并提供了相应的解决方案。
摘要由CSDN通过智能技术生成


在这里插入图片描述

测试是否springboot集成是否成功

在这里插入图片描述
相当与集成mybatis

ThymeleafController

package com.wxm.springboot.controller.springboot2;

import com.wxm.springboot.entity.springboot2.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 wxm
 * @site www.wxm.com
 * @company xxx公司
 * @create 2019-12-28 15:57
 */

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @RequestMapping("/list")
        public String  list() {
        System.out.println(" list........");
        return  "list";
    }
}

http://localhost/thymeleaf/list

在这里插入图片描述

thymeleaf模板

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

这里讲解的时候的时候是关闭缓存的(不关闭缓存有可能存在代码没有显示效果的问题)

将配置文件的后缀名改成.yml文件
在这里插入图片描述

spring.thymeleaf.cache=false

直接使用el表达式:${title},这里如果是支持的时候是会显示出人员表这几个字

在这里插入图片描述
显然不支持,因为这里使用的是html,而不是jsp,${xxx}是jstl表达式,必须基于jsp,

thymeleaf中${}语法

<h3 th:text="${title}"></h3>

在这里插入图片描述

thymeleaf中list遍历

  <tr th:each="u : ${userList}">
        <td th:text="${u.uid}">
        </td>
        <td th:text="${u.uname}">
        </td>
        <td th:text="${u.upwd}">
        </td>
    </tr>

在这里插入图片描述
在这里插入图片描述

thymeleaf解决html转译问题

html

<p th:utext="${msg}"></p>

controller
在这里插入图片描述
效果:
在这里插入图片描述

Freemarker模板

性能优化会选择Freemarker模板
学习网站:http://freemarker.foofun.cn/

Freemarker中获取单个值写法html

直接使用el表达式:${title},这里如果是支持的时候是会显示出人员表这几个字

Freemarker中条件语法html

<#if sex=='girl'>
女
<#elseif sex=='boy'>
男
<#else>
保密
</#if>

Freemarker中 exists用在逻辑判断html

<h3>exists用在逻辑判断</h3>
<#if name?exists>
    ${name}
</#if>

Freemarker中 list 循环html

<h2>循环</h2>
<table border="1px" width="600px">
    <thead>
    <tr>
        <td>ID</td>
        <td>角色名</td>
        <td>描述</td>
    </tr>
    </thead>
    <tbody>
    <#list roles as role>
    <tr>
        <td>${role.rid}</td>
        <td>${role.rname}</td>
    </tr>
    </#list>
    </tbody>
</table>

Freemarker中 局部变量(assign)/全局变量(global)html

<h2>局部变量(assign)/全局变量(global)</h2>
<#--获取全路径名${pageContext.request.contextpath}-->
<#assign ctx1>
    ${springMacroRequestContext.contextPath}
</#assign>

<#global ctx2>
    ${springMacroRequestContext.contextPath}
</#global>

pom

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

<!--可以不加,但是做项目的时候可能会用-->
 <resources>
            <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--freemarker模板也读取需要注释标红地方-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!--<include>*.properties</include>-->
                    <!--<include>*.xml</include>-->
                    <!--<include>*.yml</include>-->
                </includes>
            </resource>
        </resources>

application.yml文件的默认配置

server:
  port: 80
  servlet:
    context-path: /

spring:
  thymeleaf:
    cache: false

  freemarker:
    # 设置模板后缀名
    suffix: .ftl
    # 设置文档类型
    content-type: text/html
    # 设置页面编码格式
    charset: UTF-8
    # 设置页面缓存
    cache: false
    # 设置ftl文件路径,默认是/templates,为演示效果添加role
    template-loader-path: classpath:/templates/role
  mvc:
    static-path-pattern: /static/**

注意层级关系

相当与

springmvc.xml
     internalResouresviewResolver
         prefix  classpath:/templates/role
          subfix  .ftl

配置freemarker。也可不配置,则创建ftl文件时改后缀名

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值