栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合freemarker

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合freemarker

springboot整合freemarker
  • 1 依赖
  • 2 application.yml
  • 3 实体类
  • 4 controller
  • 5.取普通变量的值
  • 6.遍历集合
  • 7.遍历map
  • 8.转义,加r
  • 9.定义数值,且格式化数值
  • 10.定义布尔运算符
  • 11.三目运算符,用内置函数string实现
  • 12.字符串的拼接
  • 13.集合,map的拼接
  • 14.算术运算符
  • 15.比较运算符
  • 16.逻辑运算符
  • 17.分支语句
  • 18.内置函数
  • 19.如何判断某个变量为空?
  • 20.如何引入其他模板
  • 21.不解析内容
  • 22.自定义宏
  • 23 导入宏,并且使用宏

1 依赖
         
            org.springframework.boot
            spring-boot-starter-freemarker
        
2 application.yml
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    #prefix:
    suffix: .ftlh
    template-loader-path:
      - classpath:/templates/
  mvc:
    static-path-pattern: /static/**
  web:
    resources:
      static-locations: classpath:/static
3 实体类
public class User implements Serializable {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
4 controller
@Controller
public class UserController {

    @GetMapping("/hello")
    public String hello(Model model) {
        List users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(i);
            user.setName("xiaolan"+i);
            user.setAge(18);
            users.add(user);
        }
        Map map = new HashMap<>();
        map.put("name","yangl");
        map.put("age",20);
        map.put("salary",7000);
        model.addAttribute("info",map);
        model.addAttribute("sno","2020001");
        model.addAttribute("birth",new Date());
        model.addAttribute("users",users);
        return "hello";
    }
}
5.取普通变量的值
    ${sno}
6.遍历集合
   
   <#list ['hello','java','jq'] as x>
       ${x}
   
   
   
   <#list 1..5 as x>
       ${x}
   
   
   <#list 5..1 as x>
       ${x}
   

   
  <#list users as user>
      
          ${user.id}
          ${user.name}
          ${user.age}
          
          ${user_index}
          
          ${user_has_next?string('yes','no')}
      
  

   
   ${users[1].age}
   
   <#list users[2..5] as user>
       
           ${user.id}
           ${user.name}
           ${user.age}
       
   
7.遍历map
    
    <#assign usermap={"name":"小兰","age":18}>

    
    <#list usermap?keys as key>
        ${key}--${usermap[key]}
    

    
    <#list usermap?values as value>
        ${value}
    
8.转义,加r
  ${r"C:"}
    ${r"hello vue"}
9.定义数值,且格式化数值
    
    <#assign price=99>
    
    
    ${price?string.currency}
    
    ${price?string.percent}
10.定义布尔运算符
    
    <#assign flag=true>
11.三目运算符,用内置函数string实现
   
    ${flag?string('yes','no')}
12.字符串的拼接
    ${'hello ${sno}'}
    ${'hello ' + sno }
    ${sno[0]}${sno[2]}
    ${sno[1..3]}
13.集合,map的拼接
    
    
    <#list [1,2,3]+[6,8,9] as x>
        ${x}
    
    <#list (info+{"message":"soho"})?keys as key>
        ${key}
    
14.算术运算符
    
    
    <#assign num=15>
    ${num+10/1*7}
    
15.比较运算符

    
    
    <#assign i=10>
        
        <#if i = 10>i = 10
        
        <#if i != 10>i != 10
        
        <#if i == 10>i == 10
        
        <#if i gt 10>i gt 10
        
        <#if i gte 10>i gte 10
        
        <#if i lt 10>i lt 10
        
        <#if i lte 10>i lte 10
    
16.逻辑运算符
    
    
    <#assign b=20>
       <#if (b gt 10 && b lt 100)>aaa
        <#if !(b==10)>bbb
        <#if (b == 100 || 1==1)>ccc
    
17.分支语句

    
    
        <#assign score=50>
        <#if score gt 90> A
        <#elseif score gt 80>B
        <#elseif score gt 60>C
        <#else>D
        
    
    
    
        <#assign b=90>
        <#switch b>
            <#case 10>10<#break>
            <#case 20>20<#break>
            <#case 30>30<#break>
            <#default> 999
        
    
18.内置函数
 
    
    ${"hello"?cap_first}
    
    ${"hello"?lower_case}
    
    ${"hello"?upper_case}
    
    ${" hello "?trim}
    
    ${3.14566?int}
    
    ${users?size}
    
    ${birth?string("yyyy-MM-dd")}
19.如何判断某个变量为空?
    
    
    <#if qq??>
        qq
    
    
    ${dd!"dd"}
20.如何引入其他模板


21.不解析内容
 
    <#noparse >
        <#include './student.ftlh'>
        
            <#assign score=50>
            <#if score gt 90> A
            <#elseif score gt 80>B
            <#elseif score gt 60>C
            <#else>D
            
        
    
22.自定义宏

23 导入宏,并且使用宏

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号