FreeMarker Design Guide (1)

xiaoxiao2021-03-05  22

FreeMarker Design Guide (1)

1, get started

(1) Template Data Model = Output

l FreeMarker is based on designers and programmers are the concepts of different individuals with different professional skills.

l They are division of labor: designers focus - create HTML files, pictures, other visualizations of the web page; programmers create the system, generate the data to display by the design page

l The problem that is often encountered is: Information displayed in the web page (or other type of document) is invalid when the design page is based on dynamic data.

l Here, you can add some specific instructions in HTML (or other you want to output), and FreeMarker will replace these code with the appropriate data when the output page gives the end user.

l The following is an example:

Welcome! </ Title></p> <p></ hEAD></p> <p><body></p> <p><h1> Welcome $ {user}! </ h1></p> <p><P> Our Latest Product:</p> <p><a href=" # llestproduct.ur }"> $ {limitedproduct.name} </a>!</p> <p></ body></p> <p></ html></p> <p>l This example is a specific code surrounded by a simple HTML. These specific code is a directive for FreeMarker, and a file containing the freemarker is called a template (Template)</p> <p>l As for User, LatestProduct.URL and LatestProduct.name from Data Model (Data Model)</p> <p>l The data model is created by programmer, providing changes to the template, which comes from the database, file, and even directly generates in the program.</p> <p>l Template designer doesn't care about data from there, I only know that the use of data models has been used.</p> <p>l The following is a possible data model:</p> <p>(root)</p> <p>|</p> <p> - user = "big joe"</p> <p>|</p> <p> - LatestProduct</p> <p>|</p> <p> - URL = "Products / Greenmouse.html"</p> <p>|</p> <p> - Name = "Green Mouse"</p> <p>l The data model is similar to the computer's file system.</p> <p>l When FreeMarker combines the above data model into the template, the following output is created:</p> <p><html></p> <p><HEAD></p> <p><title> Welcome! </ Title></p> <p></ hEAD></p> <p><body></p> <p><H1> Welcome Big Joe! </ h1></p> <p><P> Our Latest Product:</p> <p><a href="products/greenmouse.html"> Green Mouse </a>!</p> <p></ body></p> <p></ html> (2) Data model</p> <p>l The typical data model is a tree structure, which can be arbitrarily complex and deep, as described below:</p> <p>(root)</p> <p>|</p> <p> - Animals</p> <p>| | |</p> <p>| - MOUSE</p> <p>| | | | |</p> <p>| | - SIZE = "small"</p> <p>| | | | |</p> <p>| | - Price = 50</p> <p>| | |</p> <p>| - ELEPHANT</p> <p>| | | | |</p> <p>| | - SIZE = "Large"</p> <p>| | | | |</p> <p>| | - Price = 5000</p> <p>| | |</p> <p>| - Python</p> <p>| | |</p> <p>| - SIZE = "Medium"</p> <p>| | |</p> <p>| - price = 4999</p> <p>|</p> <p> - Test = "IT IS A TEST"</p> <p>|</p> <p> - WhatNOT</p> <p>|</p> <p> - Because = "don't know"</p> <p>l Similar to the variable of the directory is called HASHES, including the only query name that saves subordinate variables</p> <p>l Similar to file variables are called Scalars, save single values</p> <p>l Scalars has two types: strings (enclosed with quotation marks, can be single or double quotes) and numbers (do not use quotes to enclose the numbers, this will be handled as a string)</p> <p>l Starting from root from root, all parts are separated from root, such as Animals.Mouse.price.</p> <p>l The other variable is SEQUENCES, and HASHES, but does not use the variable name, and the digital index is used, as described below:</p> <p>(root)</p> <p>|</p> <p> - Animals</p> <p>| | |</p> <p>| - (1st)</p> <p>| | | | |</p> <p>| | - Name = "mouse"</p> <p>| | | | |</p> <p>| | - SIZE = "small"</p> <p>| | | | |</p> <p>| | - Price = 50</p> <p>| | |</p> <p>| - (2nd)</p> <p>| | | | |</p> <p>| | - Name = "ELEPHANT"</p> <p>| | | | |</p> <p>| | - SIZE = "Large"</p> <p>| | | | |</p> <p>| | - Price = 5000</p> <p>| | |</p> <p>| - (3RD)</p> <p>| | |</p> <p>| - Name = "python"</p> <p>| | |</p> <p>| - SIZE = "Medium"</p> <p>| | |</p> <p>| - price = 4999</p> <p>|</p> <p> - WhatNOT</p> <p>|</p> <p> - fruits</p> <p>|</p> <p> - (1st) = "Orange"</p> <p>|</p> <p> - (2nd) = "banana"</p> <p>l This use index for SCALARS, such as Animals [0] .name</p> <p>(3) Template L can include the following three specific portions in the FreeMarker template:</p> <p>Ø {...}: It is called Interpolations, freeMarker is replaced with actual values ​​when output</p> <p>Ø FTL tag (FreeMarker Template Language Tag): Similar to the HTML tag, in order to distinguish between the HTML mark, use # start (starting with @, in the following description)</p> <p>Ø Note: Between <# - and -> (not <! - and ->)</p> <p>l The following is some examples of use instructions:</p> <p>Ø IF instruction</p> <p><#if animals.python.price <animals.ephant.price></p> <p>Pythons Are Cheace Than Elephants Today.</p> <p><#else></p> <p>Pythons Are Not Check Than Elephants Today.</p> <p></ # if></p> <p>Ø LIST instruction</p> <p><p> we have eachse animals:</p> <p><table border = 1></p> <p><TR> <TH> Name <TH> Price</p> <p><#List Animals As Being></p> <p><TR> <TD> $ {being.name} <td> $ {being.price} euros</p> <p></ # list></p> <p></ table></p> <p>The output is:</p> <p><p> we have eachse animals:</p> <p><table border = 1></p> <p><TR> <TH> Name <TH> Price</p> <p><TR> <TD> Mouse <TD> 50 EUROS</p> <p><TR> <TD> Elephant <TD> 5000 EUROS</p> <p><TR> <TD> Python <TD> 4999 EUROS</p> <p></ table></p> <p>Ø Include Directive</p> <p><html></p> <p><HEAD></p> <p><title> test page </ title></p> <p></ hEAD></p> <p><body></p> <p><H1> Test Page </ h1></p> <p><p> Blah Blah ...</p> <p><#include "/copyright_footer.html"></p> <p></ body></p> <p></ html></p> <p>Ø Use instructions together</p> <p><p> we have eachse animals:</p> <p><table border = 1></p> <p><TR> <TH> Name <TH> Price</p> <p><#List Animals As Being></p> <p><tr></p> <p><TD></p> <p><#if being.size = "large"> <b> </ # if></p> <p>$ {being.name}</p> <p><#if being.size = "large"> </ b> </ # if></p> <p><TD> $ {being.price} euros</p> <p></ # list></p> <p></ table></p> <p>[</p> <p>Click here to favor this article] Posted on November 02, 2004 1:23 AM</p> <p>HREF = "http://blog.9cbs.net/chenyun2000/services/pingback.aspx" Rel = "pingback" /></p> <p>A 罡 published</p> <p>2004-11-02 10:06 AM</p> <p>I am studying FreeMarker, I use it in my project, but the output of the Chinese information has become ??? this character, how to solve the Chinese problem?</p> <p>Flylyke Published</p> <p>2004-11-03 11:52 PM</p> <p>FreeMarker's support for Chinese is very good. Variables can be used in Chinese. I may have a relationship with your spring.</p> <p>Flyyue Published</p> <p>2004-11-30 11:21 am</p> <p>Use taglibs in the template of FreeMarker, always erroneous COULD NOT LOAD TAGLIB INFORMATION</p> <p>Underlying Cause:</p> <p>com.caucho.xml.xmlparseException: http://java.sun.com/dtd/web-app_2_3.dtd:1: Expected '<' At end of file</p> <p>I don't know how to solve it.</p> <p>Hotmail: flyyue@hotmail.com</p> <p>Riosober published</p> <p>2004-12-16 11:01 am</p> <p>Well, Chinese problem, just add an encodingservletfilter, add the function of the Response object to set the character set: Response.setCharacterencoding ("GBK");</p> <p>Categories:</p> <p><code></p> <p>Public class encodingservletfilter imports filter {</p> <p>Private logger logger = logger.getlogger (getclass ());</p> <p>/ **</p> <p>* <p></p> <p>* Covering method: EncodingServletfilter.destroy</p> <p>* </ p></p> <p>* <p></p> <p>* Function Description: Implementation. . .</p> <p>* </ p></p> <p>*</p> <p>* @see javax.servlet.filter # destroy ()</p> <p>* /</p> <p>Public void destroy () {</p> <p>Logger.info ("Release EncodingFilter complete!");</p> <p>}</p> <p>/ **</p> <p>* <p></p> <p>* Covering method: EncodingServletFilter.dofilter</p> <p>* </ p></p> <p>* <p></p> <p>* Function Description: Implementation. . .</p> <p>* </ p></p> <p>*</p> <p>* @see javax.servlet.filter # DOFILTER (javax.servlet.servletRequest,</p> <p>* javax.servlet.ServletResponse, javax.servlet.filterchain</p> <p>* /</p> <p>Public void Dofilter (servletRequest arg0, servletresponse arg1, filterchain arg2) throws oews oException, servletexception {</p> <p>HTTPSERVLETREQUEST REQST = (httpservletRequest) arg0;</p> <p>Arg1.setcharacterenceRencoding ("GBK");</p> <p>Arg0.SetChacTerencoding ("GBK");</p> <p>Arg1.setContentType ("text / html; charset = GBK"); arg1.getwriter (). Write ("<! " Reqst.getRequesturi () "Start ->");</p> <p>Try {</p> <p>Arg2.dofilter (arg0, arg1);</p> <p>} catch (exception e) {</p> <p>Logger.Error (Reqst.getRequesturi () "error:" E.getMessage ());</p> <p>}</p> <p>Arg1.Getwriter (). Write ("<! - End ->");</p> <p>}</p> <p>}</p> <p>/ **</p> <p>* <p></p> <p>* Coverage method: EncodingServletFilter.init</p> <p>* </ p></p> <p>* <p></p> <p>* Function Description: Implementation. . .</p> <p>* </ p></p> <p>*</p> <p>* @see javax.servlet.filter # init (javax.servlet.filterConfig)</p> <p>* /</p> <p>Public void init (filterconfig arg0) throws servletexception {</p> <p>Logger.info ("Load EncodingFilter!");</p> <p>}</p> <p>}</p> <p></ code></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-38907.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="38907" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.048</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'vqdkZcb0L2sbzjGHJX4Fm35H62dvpynJkEMlX_2FS9mgY1smzHq4NiC24xakoDfoz_2BJlo6cwBl3M_2B7LP18'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>