freemarker載入模板檔案的,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>