Java Freemarker page static instance details, javafreemarker
Freemarker
FreeMarker is a Java template engine that generates text output based on the template. FreeMarker has nothing to do with the Web Container, that is, when the Web is running, it does not know Servlet or HTTP. It can not only be used as the Implementation Technology of the presentation layer, but also be used to generate XML, JSP or Java.
Currently in Enterprises: Freemarker is mainly used for static pages or page display
Summary: Freemarker template engine, which can be used to generate html pages.
Freemarker syntax
/*** Freemark entry case * three elements of freemark: * 1. freemark API * 2. data * 3. template File: ftl file * @ throws Exception */@ Test public void test1 () throws Exception {// create freemarker core Configuration object and specify freemarker Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the server template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // specify the template File encoding cf. setDefaultEncoding ("UTF-8"); // obtain the Template file object template = c from the Template file path F. getTemplate ("hello. ftl "); // create a map Object, encapsulate the template Data Map <String, Object> maps = new HashMap <String, Object> (); maps. put ("hello", "freemarker entry case"); // create an output object and output data to the daoHtml page Writer out = new FileWriter (new File ("F: \ template \ out \ quickstart.html "); // generate an Html page template. process (maps, out); // closes the resource out. close ();}/*** freemarker template syntax to process special data formats * For example: $0.2, 20% * template syntax: $0.2 :$ {price? String. currency} * 20%: $ {price? String. percent} * @ throws Exception */@ Test public void test2 () throws Exception {// create the freemark core Configuration object and specify the freemark version Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // sets the template encoding. cf. setDefaultEncoding ("UTF-8"); // retrieves the Template file object template = cf. getTemplate ("num. ftl "); // create a map Object, encapsulate the template Data Map <String, Object> map S = new HashMap <> (); maps. put ("price", 0.2); // create an output object and output data to the Html page Writer out = new FileWriter (new File ("F: \ template \ out \ price.html "); // generate an Html page template. process (maps, out); // closes the resource out. close ();}/*** use template syntax to process null values * Template File Processing Syntax: * 1 .? * Syntax: $ {username? Default ("James")} * 2 .! * Syntax: * $ {username !} * $ {Username! "Default Value"} * 3.if * Syntax: * <# if username?> * $ {Username} * </# if> * @ throws Exception */@ Test public void test3 () throws Exception {// create freemark core configuration object, specify freemarker version Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // sets the template encoding. cf. setDefaultEncoding ("UTF-8"); // obtain the Template file object template = cf. getTemplate ("null. ftl "); // create a map object, encapsulate the template Data Map <String, Object> maps = new HashMap <> (); maps. put ("username", null); // create an output object and output data to the html page Writer out = new FileWriter (new File ("F: \ template \ out \ username.html "); // generate an html page template. process (maps, out); // closes the resource out. close ();}/*** use template syntax to process pojo data * el expression to obtain data: * model. addAttribute ("p", person); * $ {p. username} * $ {p. address} * template syntax to obtain pojo data * model. addAttribute ("p", person); * $ {p. username} * $ {p. address }* @ Throws Exception */@ Test public void test4 () throws Exception {// create the freemark core Configuration object and specify the freemarker version Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // sets the template encoding. cf. setDefaultEncoding ("UTF-8"); // obtain the Template file object template = cf. getTemplate ("person. ftl "); // create a map Object, encapsulate the template Data Map <String, Object> maps = new Hash Map <> (); // create the person object Person person = new Person (); person. setUsername ("Zhang San"); person. setAge (22); maps. put ("p", person); // create an output object and output data to the html page Writer out = new FileWriter (new File ("F: \ template \ out \ person.html "); // generate an html page template. process (maps, out); // closes the resource out. close ();}/*** use template syntax to process set data * el expression to get data: * model. addAttribute ("pList", pList); * <c: foreach item = "pList" var = "p"> * $ {p. username} * $ {P. age} * </c: foreach> * The template syntax obtains list Data * model. addAttribute ("pList", pList); * <# list pList as p> * $ {p. username} * $ {p. age} * </# list> * Syntax: $ {alias _ index} * @ throws Exception */@ Test public void test5 () throws Exception {// create freemark core Configuration object and specify freemarker version Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // sets the template Encoding cf. setDefaultEncoding ("UTF-8"); // obtain the Template file object template = cf. getTemplate ("list. ftl "); // create a map Object, encapsulate the template Data Map <String, Object> maps = new HashMap <> (); // create a list set List <Person> pList = new ArrayList <> (); // create the person object Person person1 = new Person (); person1.setUsername ("James "); person1.setAge (22); // create the person object Person person2 = new Person (); person2.setUsername (""); person2.setAge (24); pList. Add (person1); pList. add (person2); maps. put ("pList", pList); // create an output object and output the data to the html page Writer out = new FileWriter (new File ("F: \ template \ out \ list.html "); // generate an html page template. process (maps, out); // closes the resource out. close ();}/*** use template syntax to process time-type data * Get Data Date: $ {today? Date} * Data Acquisition Time: $ {today? Time} * Get Data Date and time: $ {today? Datetime} * format the date and time of the retrieved data: $ {today? String ('yyyy-MM-dd')} * @ throws Exception */@ Test public void test6 () throws Exception {// create freemark core configuration object, specify freemarker version Configuration cf = new Configuration (Configuration. getVersion (); // specifies the path of the template file. setDirectoryForTemplateLoading (new File ("F: \ template"); // sets the template encoding. cf. setDefaultEncoding ("UTF-8"); // obtain the Template file object template = cf. getTemplate ("date. ftl "); // create a map Object, encapsulate the template Data Map <String, Object> maps = new HashMap <> (); maps. put ("today", new Date (); // create an output object and output the data to the html page Writer out = new FileWriter (new File ("F: \ template \ out \ date.html "); // generate an html page template. process (maps, out); // closes the resource out. close ();}
Introduction page
The following command can be used to import another page to this page:
Jsp introduction page:
Ftl introduction page: <# include "/include/head. ftl">
Freemarker integrates spring
Configuration integration Freemarker spring configuration file:
<! -- Freemarker handed over to spring Management --> <! -- Use spring to provide a template to manage configuration objects --> <bean class = "org. springframework. web. servlet. view. freemarker. FreeMarkerConfigurer"> <! -- Template path --> <property name = "templateLoaderPath" value = "/WEB-INF/fm/"/> <! -- Template encoding --> <property name = "defaultEncoding" value = "UTF-8"/> </bean>
Create a template object
Freemarker is placed under the server: WEB-INF Folder: access the resource file, the server must be started.
Load the spring configuration file of the application in web. xml:
<! -- Load springmvc --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: springmvc. xml, classpath: applicationContext -*. xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet>
Nginx access
Direct access, unable to load style resources, must go through the http server to Load Static resources.
Nginx serves as an http server to access static resources.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!