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

【Java从零到架构师第③季】【45】SpringBoot-Freemarker

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

【Java从零到架构师第③季】【45】SpringBoot-Freemarker


持续学习&持续更新中…

守破离


【Java从零到架构师第③季】【45】SpringBoot-Freemarker
    • 页面模板
    • Freemarker
    • Freemarker—HelloWorld
    • Freemarker—生成Mapper.java
    • Freemarker—注释
    • 集成到SpringBoot
    • 参考

页面模板

Freemarker

http://freemarker.foofun.cn/

Freemarker—HelloWorld


    org.freemarker
    freemarker
    2.3.29

mapper.ftl:



    title


${name}

${age}

    public static void main(String[] args) throws Exception {
        final String basePath = "D:/code_space/LearnJava/IntellijIdea/LearnJavaEEWithXMGLMJ/StepTHREE/SpringBoot-Freemarker/";
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setDirectoryForTemplateLoading(new File(basePath + "templates"));
        Template tpl = cfg.getTemplate("mapper.ftl");
        Map data = new HashMap<>();
        data.put("name", "lp");
        data.put("age", 10);
        try (FileWriter out = new FileWriter(new File(basePath + "src/main/resources/static/index.html"))) {
            tpl.process(data, out);
        }
    }
Freemarker—生成Mapper.java

mapper.ftl:

package programmer.lp.fm.mapper;

import programmer.lp.fm.pojo.po.${type};

public interface ${type}Mapper extends BaseMapper<${type}> {

}

Main.main():

    public static void main(String[] args) throws Exception {
        final String basePath = "D:/code_space/LearnJava/IntellijIdea/LearnJavaEEWithXMGLMJ/StepTHREE/SpringBoot-Freemarker/";

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        // 设置编码
        cfg.setDefaultEncoding("UTF-8");
        // 模板文件的存放目录
        cfg.setDirectoryForTemplateLoading(new File(basePath + "templates"));

        // 获取模板文件
        Template tpl = cfg.getTemplate("mapper.ftl");

        // 数据
        Map data = new HashMap<>();
        final String className = "DictItem";
        data.put("type", className);

        try (FileWriter out = new FileWriter(new File(basePath + "src/main/java/programmer/lp/fm/mapper/" + className + "Mapper.java"))) {
            tpl.process(data, out);
        }
    }
Freemarker—注释

集成到SpringBoot

pom.xml:



    org.springframework.boot
    spring-boot-starter-freemarker

application.yml:

@Controller
@RequestMapping("/dictTypes")
public class DictTypeController {
    @Autowired
    private DictTypeService service;

    @GetMapping("/list")
    public String list(Model model) {
        final List list = service.list();
        model.addAttribute("data", list);
        return "pages/dictType";
    }
}

resources/templates/pages/dictType.ftlh:

<#assign ctx="${springMacroRequestContext.getContextPath()}">









<#list data as item>
    
        
        ${item.name}
        ${item.value}
        
        ${item.intro!}
        
        
    

参考

小码哥-李明杰: Java从0到架构师③进阶互联网架构师.


本文完,感谢您的关注支持!


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

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

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