• Springboot 视图渲染技术(Freemarker)


    目录

    1.  freemarker介绍

    2. freemarker使用步骤

     2.1 在pom.xml引入freeMarker的依赖包 

     2.2 在springboot中添加freemarker配置

     2.3 编写模板文件*.ftl

     2.3 设置模板文件*.ftl 语法

     2.4 访问控制器后进行页面跳转 

    3. freemarker常用语法

     3.1 将以下内容添加IndexController文件里面

     3.2 将以下内容添加到 创建好的 index 模板页面里面


    1.  freemarker介绍

    FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据,并用来生成输出文(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 

    • 注1:springboot中支持的两种模板技术 thymeleaf/freemarker(默认) 

    2. freemarker使用步骤

      2.1 在pom.xml引入freeMarker的依赖包 

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-freemarkerartifactId>
    4. dependency>

      2.2 在springboot中添加freemarker配置

       将以下内容添加到application.properties配置文件里面 

    1. #配置jdbc
    2. spring:
    3. #配置freemarker
    4. freemarker:
    5. #指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
    6. allow-request-override: false
    7. #req访问request
    8. request-context-attribute: req
    9. #后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
    10. suffix: .ftl
    11. #设置响应的内容类型
    12. content-type: text/html;charset=utf-8
    13. #是否允许mvc使用freemarker
    14. enabled: true
    15. #是否开启template caching
    16. cache: false
    17. #设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
    18. template-loader-path: classpath:/templates/
    19. #设定Template的编码
    20. charset: UTF-8
    21. # 设置静态文件路径,js,css等
    22. mvc:
    23. static-path-pattern: /static/**

      2.3 编写模板文件*.ftl

       File --> Settings

     

     

     2.3 设置模板文件*.ftl 语法

      File --> Settings

     

     

     2.4 访问控制器后进行页面跳转 

      2.4.1 在pom.xml引入springweb的相关依赖包

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.projectlombokgroupId>
    7. <artifactId>lombokartifactId>
    8. <optional>trueoptional>
    9. dependency>

      2.4.2 新建一个freemaker模板

      src/man/resources/templates目录下新建一个freemarker模板 

     

     

     2.4.3 编写Controller层代码 名称:IndexController

    1. package com.jmh.springboot03.controller;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RequestParam;
    7. import org.springframework.web.servlet.ModelAndView;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. /**
    11. * @author 蒋明辉
    12. * @data 2022/9/29 12:40
    13. */
    14. @Controller
    15. public class IndexController {
    16. @RequestMapping("/") //里面路径为/ 会在启动项目之后自动访问此方法
    17. public ModelAndView index(){
    18. //实例一个ModelAndView
    19. //作用即可作为作用域保存数据也可以作为页面跳转等等...
    20. ModelAndView model=new ModelAndView();
    21. model.setViewName("index");//跳转视图名
    22. return model;
    23. }
    24. }

     2.4.4 进入主类启动服务器

     2.4.5 在网页地址栏输入 localhost:8080/ 访问项目

     

    3. freemarker常用语法

     3.1 将以下内容添加IndexController文件里面

    1. package com.jmh.springboot03.controller;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RequestParam;
    7. import org.springframework.web.servlet.ModelAndView;
    8. import java.util.ArrayList;
    9. import java.util.List;
    10. /**
    11. * @author 蒋明辉
    12. * @data 2022/9/29 12:40
    13. */
    14. @Controller
    15. public class IndexController {
    16. //定义一个内部类
    17. @Data
    18. @AllArgsConstructor
    19. public class student{
    20. private Integer id;
    21. private String name;
    22. private float price;
    23. }
    24. @RequestMapping("/")
    25. public ModelAndView index(){
    26. ModelAndView model=new ModelAndView();
    27. model.addObject("name","蒋明辉");
    28. model.addObject("sex","男");
    29. //数组
    30. model.addObject("arrays",new String[]{"zs","ls","ww"});
    31. //集合
    32. List studentList=new ArrayList<>();
    33. studentList.add(new student(1,"zs",12f));
    34. studentList.add(new student(2,"ls",13f));
    35. studentList.add(new student(3,"ww",14f));
    36. studentList.add(new student(4,"ff",15f));
    37. model.addObject("list",studentList);
    38. model.setViewName("index");
    39. return model;
    40. }
    41. }

      3.2 将以下内容添加到 创建好的 index 模板页面里面

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>title>
    6. head>
    7. <body>
    8. <h1>springboot渲染技术 CTO${name!'董事长'}h1>
    9. <h3>1)exists用在逻辑判断;h3>
    10. <#if name?exists>
    11. ${name}
    12. <h3>2)if_exists用来打印东西h3>
    13. ${name?if_exists}
    14. <h3>3)??是判断对象是否为空h3>
    15. <#if name??>
    16. ${name}
    17. <h3>4)if条件h3>
    18. <#if sex='男'>
    19. 是男的
    20. <#elseif sex='女'>
    21. 是女的
    22. <#else >
    23. 是人妖
    24. <h3>5)循环01 数组h3>
    25. <#list arrays as it>
    26. ${it}
    27. <h3>6)循环02 集合h3>
    28. <#list list as li>
    29. 编号:${li.id}
    30. 名称:${li.name}
    31. 价格:${li.price}
    32. <h3>7)include全局变量&局部变量h3>
    33. <#include "common/head.ftl">
    34. 局部变量:${o}
    35. 全局变量:${t}
    36. <div style="margin-bottom: 300px">div>
    37. body>
    38. html>

      每一个freemarker语法都标有提示注释 想了解的就去好好的看一下就明白 简单易懂

  • 相关阅读:
    python操作excel中xlrd模块的一些简单方法
    UVaLive 6693 Flow Game (计算几何,线段相交)
    vxe是一款功能强大的table,有vue2/3版本
    MySQL库表操作
    LLM App ≈ 数据ETL管线
    电容屏物体识别手工制作
    ZooKeeper 7:数据读写——原子广播协议ZAB
    MQTT X 1.9.0 发布:开箱即用的 bench 命令,MQTT 性能测试更便捷
    windiws docker 部署jar window部署docker 转载
    System Generator学习——将代码导入System Generator
  • 原文地址:https://blog.csdn.net/m0_63300795/article/details/127112947