使用Freemarker自动生成对应的代码(基于Spring)

Freemarker

  1. 在Maven工程中导入依赖
  2. 在spring中配置对应的bean
  3. 准备想要生成的模板
  4. 使用Java代码实现自动生成
在Maven工程中导入依赖

我这里导入的不是最新版本的,是按使用人数较多的来导入的。

<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
在spring配置文件中配置对应的bean
	<!--扫描输出目录的配置文件,用来指定生成的静态页面存放到哪个目录-->
	<context:property-placeholder location="classpath*:*.properties"/>
    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <!--指定模板文件所在目录-->
        <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <!--指定字符集-->
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

freemarker.properties(用来存放生成的静态页面)

out_put_path=F://IDEACode//pages
准备想要生成的模板

最好使用ftl结尾,比较规范
在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no,minimal-ui">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../img/asset-favico.ico">
    <title>预约</title>
    <link rel="stylesheet" href="../css/page-health-order.css" />
</head>
<body data-spy="scroll" data-target="#myNavbar" data-offset="150">
<div class="app" id="app">
    <!-- 页面头部 -->
    <div class="top-header">
        <span class="f-left"><i class="icon-back" onclick="history.go(-1)"></i></span>
        <span class="center">测试页面</span>
        <span class="f-right"><i class="icon-more"></i></span>
    </div>
    <!-- 页面内容 -->
    <div class="contentBox">
        <div class="list-column1">
            <ul class="list">
                <#list setmealList as setmeal>
                    <li class="list-item">
                        <a class="link-page" href="setmeal_detail_${setmeal.id}.html">
                            <img class="img-object f-left"
                                 src="http://qemgg59ts.bkt.clouddn.com/${setmeal.img}"
                                 alt="">
                            <div class="item-body">
                                <h4 class="ellipsis item-title">${setmeal.name}</h4>
                                <p class="ellipsis-more item-desc">${setmeal.remark}</p>
                                <p class="item-keywords">
                                    <span>
                                        <#if setmeal.sex == '0'>
                                            性别不限
                                        <#else>
                                            <#if setmeal.sex == '1'>
                                                男
                                            <#else></#if>
                                        </#if>
                                    </span>
                                    <span>${setmeal.age}</span>
                                </p>
                            </div>
                        </a>
                    </li>
                </#list>
            </ul>
        </div>
    </div>
</div>
</body>
使用Java代码实现自动生成
	//自动装配
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
/**
     * 实现自动生成静态页面
     * @param tempateName 要生成模版的名称
     * @param outputname  要生成的静态页面名称,以.html结尾
     * @param map         传入页面需要的数据(页面定义了插值,将对应的数据传过去)
     */
    public void generateHtml(String tempateName, String outputname, Map map)  {
        File file = null;
        Writer out = null;
        try {
            Configuration configuration = freeMarkerConfigurer.getConfiguration();

            Template template = configuration.getTemplate(tempateName);
            out = new BufferedWriter(new FileWriter(new File(outPutPath+"\\"+outputname)));
            template.process(map, out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

以上就是本次分享全部内容,如果有不恰当的地方,欢迎指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值