Freemarker loads the template file, and freemarker loads the Template

Source: Internet
Author: User

Freemarker loads the template file, and freemarker loads the Template

Java code:

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

// Home file path + name (Template File name) paramMap contains (parameter values of page parameters)
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); // The page parameters interact with the template file.
Content = writer. toString ();
} Catch (Exception ex ){
Log. error ("Cannot get schema content {}! ", Name, ex );
}
Return content;
}

Template File: (xml file)

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

Template File: table_form (ftl file)

<? 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 ("query", "query failed! ");
}
}, Function (){
ShowErrorDialog ("query", "query failed. Unknown network error! ");
});
}
Function showAddDialog ($ {tid }){
$ {Fid} Form. clearData ();
$ {Yid} Dialog. setTitle ("add ");
$ {Yid} Dialog. setBtnTitle (0, "add ");
$ {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 ("add", "failed to add. The name already exists! ");
} Else {
ShowErrorDialog ("add", "added successfully! ");
Query ($ {tid });
}
} Else {
ShowErrorDialog ("add", "failed to add! ");
}
}, Function (){
ShowErrorDialog ("add", "failed to add. Unknown network error! ");
})
}
};
$ {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 ("edit ");
$ {Yid} Dialog. setBtnTitle (0, "save ");
$ {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 ("edit", "edited! ");
$ {Tid}. data [index] = formData;
$ {Tid}. refresh ();
} Else {
ShowErrorDialog ("edit", "Edit failed. The name already exists! ");
}
} Else {
ShowErrorDialog ("edit", "Edit failed! ");
}
}, Function (){
ShowErrorDialog ("edit", "Edit failed. Unknown network error! ");
})
}
};
$ {Yid} Dialog. clickBtn (1, function (){
$ {Yid} Dialog. hide ();
});
$ {Yid} Dialog. show ();
}
Function showRemoveDialog ($ {tid}, index ){
$ {Gid} Dialog. setTitle ("delete ");
$ {Gid} Dialog. setContent ("<br/> are you sure you want to delete it? ");
$ {Gid} Dialog. setBtnTitle (0, "delete ");
$ {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 ("delete", "deleted successfully! ");
$ {Tid}. remove (index );
Query ($ {tid });
} Else {
ShowErrorDialog ("delete", "deletion failed! ");
}
}, Function (){
ShowErrorDialog ("delete", "failed to delete, Unknown network error! ");
});
}
$ {Gid} Dialog. clickBtn (1, function (){
$ {Gid} Dialog. hide ();
});
$ {Gid} Dialog. show ();
}
Function showErrorDialog (title, message ){
$ {Gid} Dialog. setTitle (title );
$ {Gid} Dialog. setContent ("<br/>" + message );
$ {Gid} Dialog. setBtnTitle (0, "close ");
$ {Gid} Dialog. hideBtn (1 );
$ {Gid} Dialog. clickBtn (0, function (){
$ {Gid} Dialog. hide ();
});
$ (Document). keydown (function (event ){
$ {Gid} Dialog. closeBtn (event );
});
$ {Gid} Dialog. show ();
}
]> </Yscript>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.