freemarker模板使用

1.导入插件到eclipse安装目录dropins
这里写图片描述
2.在需要使用模板的项目的web-inf文件夹下创建文件夹保存模板文件
这里写图片描述
3.模板编写,简单模板

<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>

4.创建模板生产文件夹
这里写图片描述
5.查询数据库,生产数据,写入模板文件,生产静态页面(结合AngularJS路由)
5.1 页面路径传递查询数据的id
这里写图片描述
5.2路由页面接收参数
这里写图片描述
5.3编写action

    @Action(value="promotion_html")
    public String creatHtml() throws IOException, Exception {
        //1.获取写出模板文件地址
        String html = ServletActionContext.getServletContext().getRealPath("promotion");
        //2.创建文件
        File htmlFile = new File(html,model.getId()+".html");
        if(!htmlFile.exists()) {
            //1.获取模板存放路径
            String file = ServletActionContext.getServletContext().getRealPath("WEB-INF/freemarkertemplate");
            //2.设置版本
            Configuration configuration = new Configuration(
                    Configuration.VERSION_2_3_22);
            //3.加载模板地址
            configuration.setDirectoryForTemplateLoading(new File(file));
            //4.加载模板
            Template template = configuration.getTemplate("freemarker.ftl");
            //5.查询数据,调用webservice服务获取其他服务器数据
            Promotion promotion = WebClient.create("http://localhost:8081/bos_management/services/promotionService/getPromotion/"+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 FileWriter(htmlFile));
        }
        //6.把生产页面响应回页面
        FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());
        return NONE;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值