freemarker加载模板文件的
2017-12-05 20:34
429 查看
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 = {};
4000
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>
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 = {};
4000
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>
相关文章推荐
- springMVC加载远程freemarker模板文件
- FreeMarer 模板加载,使用FreeMarker加载远程主机上模板文件
- 使用FreeMarker加载远程主机上模板文件,比如FTP,Hadoop等(转载)
- 使用FreeMarker加载远程主机上模板文件,比如FTP,Hadoop等
- freemarker加载模板文件的三种方法
- freemarker加载模板文件的三种方法
- FreeMarker模板文件的组成及基本语法详解(二)
- 【FreeMarker】【模板文件FTL】模板表达式
- 【FreeMarker】【模板文件FTL】【参考文档】保留字
- 解决 IntelliJ IDEA 无法热加载 Spring Boot 模板文件和静态资源文件
- PHP - Smarty模板引擎 - Manual手册 - Chapter 7. Built-in Functions第7章 内建函数 - {config_load}用于从一个配置文件加载配置变量到模板中
- Java使用freemarker模板导出pdf文件
- TP模板加载和导入外部文件
- thinkphp模板加载样式文件
- java freemarker 通过ftl模板文件导出word文件发现在有中文地方出现在乱码,打开word文件提示xml错误解决办法
- Freemarker提供了3种加载模板目录的方法
- 解决 IntelliJ IDEA 无法热加载 Spring Boot 模板文件和静态资源文件
- FreeMarker模板文件的组成及基本语法详解(一)
- Golang 页面模板之间的嵌套定义以及静态文件的加载方式
- 在freemarker的模板文件中调用自定义的方法