zoukankan      html  css  js  c++  java
  • freemarker加载模板文件的

    java代码:

    public String getContent(String name, HashMap<String, Object> paramMap) {

    //home 文件路径 + name (模版文件名称)  paramMap包含(页面参过来的参数值)
    String content = "";
    try {
    paramMap.put("name", name);
    paramMap.put("templ", this);
    paramMap.put("param", paramMap);
    this.paramMap = paramMap;

    Configuration configuration = new Configuration();
    configuration.setDirectoryForTemplateLoading(ZDZServiceManager.getEnv().getHomeFile(home));
    configuration.setObjectWrapper(new DefaultObjectWrapper());
    configuration.setDefaultEncoding("UTF-8");

    Template template = configuration.getTemplate(name);

    StringWriter writer = new StringWriter();
    template.process(paramMap, writer);//页面参数与模版文件交互
    content = writer.toString();
    } catch (Exception ex) {
    log.error("Cannot get schema content {} !", name, ex);
    }
    return content;
    }

    模版文件:(xml文件)

    <?xml version="1.0" encoding="UTF-8"?>
    <table total="547" pager="50" page="0">
    <#assign queryList=json.query>
    <#assign queryBtnList=json.queryBtn>
    <#assign tableList=json.table>
    <#if json.tableBtn??>
    <#assign tableBtnList=json.tableBtn>
    </#if>
    <#if queryList??&&(queryList?size>0)>
    <query>
    <#list queryList as query>
    <field type="${query.type}" id= "eq_${query.value}" title="${query.title}" width="${query.width}" color="${query.color}"/>
    </#list>
    <#list queryBtnList as queryBtn>
    <button color="${queryBtn.color}" href="${queryBtn.href}" title="${queryBtn.title}" />
    </#list>
    </query>
    </#if>
    <#list tableList as table>
    <field type="${table.type}" id="${table.value}" title="${table.title}" width="${table.width}" color="${table.color}"/>
    </#list>
    <#if tableBtnList??&&(tableBtnList?size>0)>
    <#list tableBtnList as tableBtn>
    <#list tableBtn as field>
    <#if field_index==0>
    <field type="button" id="" title="${field.fieldTitle}" width="" color="${field.fieldColor}">
    <#list tableBtn as field>
    <#if field_index!=0>
    <button color="${field.color}" href="${field.href}" title="${field.title}" />
    </#if>
    </#list>
    </field>
    </#if>
    </#list>
    </#list>
    </#if>
    </table>

    模版文件:table_form(ftl文件)

    <?xml version="1.0" encoding="UTF-8"?>
    <ypattern>
    <#assign tid = templ.getNextId()>
    <#assign configName = templ.getConfigName(viewId,tid,".xml")>
    <#assign code = templ.writeChildContent(configName,"table/table_config.xml")>
    <ytable id="${tid}" config="${configName}" width="86%" style="brown"/>

    <#assign yid = templ.getNextId()>
    <#assign configName = templ.getConfigName(viewId,yid,".xml")>
    <#assign code = templ.writeChildContent(configName,"table/dialog_config.xml")>
    <ydialog id="${yid}" config="${configName}" visible="0" size="0">

    <#assign fid = templ.getNextId()>
    <#assign configName = templ.getConfigName(viewId,fid,".xml")>
    <#assign code = templ.writeChildContent(configName,"table/form_config.xml")>
    <yform id="${fid}" config="${configName}" />
    </ydialog>

    <#assign gid = templ.getNextId()>
    <#assign configName = templ.getConfigName(viewId,gid,".xml")>
    <#assign code = templ.writeChildContent(configName,"table/dialog_config.xml")>
    <ydialog id="${gid}" config="${configName}" visible="0" size="0"></ydialog>
    <yscript><![CDATA[
    $(document).ready(function(){
    query(${tid}Table);
    });
    function query(${tid}) {
    var url = "${json.url.query}";
    var filter = ${tid}.getFilter();
    filter.start = ${tid}.page;
    filter.max = ${tid}.pageMax;
    $z.showSpin();
    $z.post(url,filter,function(data) {
    if(data.code >= 0) {
    ${tid}.removeAll();
    ${tid}.add(data.result);
    ${tid}.showPager(url,filter,data.count,data.page,data.pageMax);
    ${tid}.refresh();
    } else {
    showErrorDialog("查询","查询失败!");
    }
    }, function() {
    showErrorDialog("查询","查询失败,未知的网络错误!");
    });
    }
    function showAddDialog(${tid}) {
    ${fid}Form.clearData();
    ${yid}Dialog.setTitle("添加");
    ${yid}Dialog.setBtnTitle(0,"添加");
    ${yid}Dialog.clickBtn(0,addDialog);
    $(document).keydown(function(event){
    ${yid}Dialog.keyBtn(event,addDialog);
    });
    function addDialog(){
    if(${fid}Form.validate()) {
    var formData = ${fid}Form.getData();
    $z.showSpin();
    $z.post("${json.url.add}",formData,function(data) {
    if(data.code >= 0) {
    ${yid}Dialog.hide();
    if(data.result == -1) {
    showErrorDialog("添加","添加失败,名称已经存在!");
    } else {
    showErrorDialog("添加","添加成功!");
    query(${tid});
    }
    } else {
    showErrorDialog("添加","添加失败!");
    }
    }, function() {
    showErrorDialog("添加","添加失败,未知的网络错误!");
    })
    }
    };
    ${yid}Dialog.clickBtn(1,function(){
    ${yid}Dialog.hide();
    });
    ${yid}Dialog.show();
    };
    function showEditDialog(${tid},index) {
    var rec = ${tid}.data[index];
    ${fid}Form.setData(rec);
    ${yid}Dialog.setTitle("编辑");
    ${yid}Dialog.setBtnTitle(0,"保存");
    ${yid}Dialog.clickBtn(0,editDialog);
    $(document).keydown(function(event){
    ${yid}Dialog.keyBtn(event,editDialog);
    });
    function editDialog(){
    if(${fid}Form.validate()) {
    var formData = ${fid}Form.getData(rec);
    formData.eq_id = rec.id;
    $z.showSpin();
    $z.post("${json.url.edit}",formData,function(data) {
    if(data.code >= 0) {
    if(data.result) {
    ${yid}Dialog.hide();
    showErrorDialog("编辑","编辑成功!");
    ${tid}.data[index] = formData;
    ${tid}.refresh();
    } else {
    showErrorDialog("编辑","编辑失败,名称已经存在!");
    }
    } else {
    showErrorDialog("编辑","编辑失败!");
    }
    }, function() {
    showErrorDialog("编辑","编辑失败,未知的网络错误!");
    })
    }
    };
    ${yid}Dialog.clickBtn(1,function(){
    ${yid}Dialog.hide();
    });
    ${yid}Dialog.show();
    }
    function showRemoveDialog(${tid},index) {
    ${gid}Dialog.setTitle("删除");
    ${gid}Dialog.setContent("<br/><br/>确认要删除吗?");
    ${gid}Dialog.setBtnTitle(0,"删除");
    ${gid}Dialog.showBtn(1);
    $(document).keydown(function(event){
    ${gid}Dialog.keyBtn(event,removeDialog);
    });
    ${gid}Dialog.clickBtn(0,removeDialog);
    function removeDialog() {
    ${gid}Dialog.hide();
    var formData = {};
    formData.eq_id = ${tid}.data[index].id;
    $z.showSpin();
    $z.post("${json.url.del}",formData,function(data) {
    if(data.code >= 0) {
    ${yid}Dialog.hide();
    showErrorDialog("删除","删除成功!");
    ${tid}.remove(index);
    query(${tid});
    } else {
    showErrorDialog("删除","删除失败!");
    }
    }, function() {
    showErrorDialog("删除","删除失败,未知的网络错误!");
    });
    }
    ${gid}Dialog.clickBtn(1,function(){
    ${gid}Dialog.hide();
    });
    ${gid}Dialog.show();
    }
    function showErrorDialog(title, message) {
    ${gid}Dialog.setTitle(title);
    ${gid}Dialog.setContent("<br/><br/>" + message);
    ${gid}Dialog.setBtnTitle(0,"关闭");
    ${gid}Dialog.hideBtn(1);
    ${gid}Dialog.clickBtn(0,function(){
    ${gid}Dialog.hide();
    });
    $(document).keydown(function(event){
    ${gid}Dialog.closeBtn(event);
    });
    ${gid}Dialog.show();
    }
    ]]></yscript>

  • 相关阅读:
    SQL进阶总结(二)
    SQL进阶总结(一)
    Springmvc学习(三)——响应数据和结果视图
    springMVC学习(二)——常用注解
    Springmvc学习(一)——简单入门案例
    Nginx解决前端访问资源跨域问题
    MySQL-安装配置篇
    Centos下的MySQL安装及配置
    关于Java 值传递深度分析
    c#统计代码行数
  • 原文地址:https://www.cnblogs.com/wylblogs/p/freemarker.html
Copyright © 2011-2022 走看看