freemarker載入模板檔案的,freemarker載入模板

來源:互聯網
上載者:User

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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.