freemark使用

实现步骤:
1、创建Configuration,生成模板实例

Configuration configuration = new Configuration();
    configuration.setDirectoryForTemplateLoading(new File("ftl文件位置"));

2、指定使用模板文件,生成Template实例

Template template = configuration.getTemplate("flt文件名");

3、填充数据模型,数据模型就是一个Map

Map<String, String> map = new HashMap<String, String>();
    map.put(key, value);

4、调用Template实例process完成数据合并

template.process(map, 目标Writer输出流);

利用freemarker模板实现系统详情步骤

在WEB-INF目录下创建保存模板的文件夹

编辑freemarker的模板文件
通常模板文件放在WEB-INF下,或者classes下
xxx.ftl
{变量},需要在程序中指定变量,合并到模板文件一同输出

<link rel="stylesheet" type="text/css" href="css/promotion_detail.css">
<div class="container promotions" >
    <div class="col-md-2 prolist">
        <h5 class="title"><a href="#/promotion"><strong>返回首页</strong></a></h5>
        <img src="images/pro.jpg" class="img-responsive">
    </div>
    <div class="col-md-10 procontent">
        <h5 class="title">${promotion.title}</h5>
        <div class="intro">
            <p>描述: ${promotion.activeScope}</p>
            <p>日期: ${promotion.startDate?string("yyyy-MM-dd")} - 
                ${promotion.endDate?string("yyyy-MM-dd")}</p>
        </div>
        <div class="partline clearfix"></div>
        <div class="promotionbox">
            ${promotion.description}
        </div>
    </div>
</div>

在WebAPP下创建文件夹用来保存通过freemarker模板生成html网页后保存的路径

修改.html中的点击事件

<div class="col-sm-6 col-md-3" ng-repeat="item in pageItems">
    <div class="thumbnail">
        <img ng-src="{{item.titleImg}}" alt="活动一">
            <div class="caption">
                <p><a href="#/promotion_detail/{{item.id}}">{{item.title}}</a></p>
                    <p class="text-right status">
                        <span ng-show="item.status == '1'">进行中</span>
                            <span ng-show="item.status == '2'">已结束</span>
                    </p>
                <p class="text-right grey">{{item.startDate}}—{{item.endDate}}</p>
            <p class="text-right grey">{{item.activeScope}}</p>
        </div>
    </div>
</div>

编写Action实现模板数据的逻辑处理
编写freemarker模板动态生成html网页实现

@Action(value = "promotion_showDetail")
    public String showDetail() throws Exception {
        // 先判断 id 对应html 是否存在,如果存在 直接返回
        String htmlRealPath = ServletActionContext.getServletContext()
                .getRealPath("/freemarker");
        File htmlFile = new File(htmlRealPath + "/" + model.getId() + ".html");

        // 如果html文件不存在 ,查询数据库,结合freemarker模板生成 页面
        if (!htmlFile.exists()) {
            Configuration configuration = new Configuration(
                    Configuration.VERSION_2_3_22);
            configuration.setDirectoryForTemplateLoading(new File(
                    ServletActionContext.getServletContext().getRealPath(
                            "/WEB-INF/freemarker_templates")));
            // 获取模板对象
            Template template = configuration
                    .getTemplate("promotion_detail.ftl");

            // 动态数据
            Promotion promotion = WebClient
                    .create(Constants.BOS_MANAGEMENT_URL
                            + "/ikayaki_bos_management/services/promotionService/promotion/"
                            + model.getId()).accept(MediaType.APPLICATION_JSON)
                    .get(Promotion.class);
            Map<String, Object> parameterMap = new HashMap<String, Object>();
            parameterMap.put("promotion", promotion);

            // 合并输出
            template.process(parameterMap, new OutputStreamWriter(
                    new FileOutputStream(htmlFile), "utf-8"));

        }

        // 存在 ,直接将文件返回
        ServletActionContext.getResponse().setContentType(
                "text/html;charset=utf-8");
        FileUtils.copyFile(htmlFile, ServletActionContext.getResponse()
                .getOutputStream());

        return NONE;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值