Freemarker
FreeMarker is a template engine written in Java language that generates text output based on templates. FreeMarker has nothing to do with web containers, i.e. when the web is running, it does not know about Servlets or HTTP. It can not only be used as an implementation technology for presentation layers, but also for generating XML, JSP, or Java, etc.
Currently in enterprises: Freemarker is mainly used for static pages or page displays
Summarize: freemarker template engine, you can use Freemarker templates to generate html pages.
Freemarker syntax
/** * Freemark Introduction Case * Freemark three elements: * API * 2. Data * 3. Template file: ftl file * @throws Exception */ @Test public void test1() throws Exception{ //Create a freemarker core configuration object and specify a freemarker Configuration cf = new Configuration(()); //Specify the path of the server template file (new File("F:\\template")); //Specify template file encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<String,Object>(); ("hello", "Freemarker Beginner Case"); //Create an output object and output the data to daoHtml page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate HTML page (maps, out); //Close resources (); } /** * freemarker template syntax processing special data format *Example: $0.2,20% * Template syntax: $0.2:${price?} * 20%:${price?} * @throws Exception */ @Test public void test2() throws Exception{ //Create a freemark core configuration object and specify the freemark version Configuration cf = new Configuration(()); //Specify the path to the template file (new File("F:\\template")); //Set template encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<>(); ("price", 0.2); //Create the output object and output the data to the HTML page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate HTML page (maps, out); //Close resources (); } /** * Use template syntax to process null values * Template file processing syntax: * 1.? * Syntax: ${username?default("Zhang San")} * 2.! * grammar: * ${username!} * ${username!"Default value"} * * grammar: * <#if username??> * ${username} * </#if> * @throws Exception */ @Test public void test3() throws Exception{ //Create a freemark core configuration object and specify the freemarker version Configuration cf = new Configuration(()); //Specify the path to the template file (new File("F:\\template")); //Set template encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<>(); ("username", null); //Create the output object and output the data to the html page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate html page (maps, out); //Close resources (); } /** * Use template syntax to process pojo data * el expression gets data: * ("p",person); * ${} * ${} * Template syntax to get pojo data * ("p",person); * ${} * ${} * @throws Exception */ @Test public void test4() throws Exception{ //Create a freemark core configuration object and specify the freemarker version Configuration cf = new Configuration(()); //Specify the path to the template file (new File("F:\\template")); //Set template encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<>(); //Create a person object Person person = new Person(); ("Zhang San"); (22); ("p", person); //Create the output object and output the data to the html page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate html page (maps, out); //Close resources (); } /** * Use template syntax to process collection data * el expression gets data: * ("pList",pList); * <c:foreach item="pList" var="p"> * ${} * ${} * </c:foreach> * Template syntax to get list data * ("pList",pList); * <#list pList as p> * ${} * ${} * </#list> * Angle tag syntax: ${alias_index} * @throws Exception */ @Test public void test5() throws Exception{ //Create a freemark core configuration object and specify the freemarker version Configuration cf = new Configuration(()); //Specify the path to the template file (new File("F:\\template")); //Set template encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<>(); //Create list collection List<Person> pList = new ArrayList<>(); //Create a person object Person person1 = new Person(); ("Zhang San"); (22); //Create a person object Person person2 = new Person(); ("Li Si"); (24); (person1); (person2); ("pList", pList); //Create the output object and output the data to the html page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate html page (maps, out); //Close resources (); } /** * Use template syntax to process time type data * Get data date: ${today?date} * Get data time: ${today?time} * Get data date and time: ${today?datetime} * Get data date and time format: ${today?string('yyyy-MM-dd')} * @throws Exception */ @Test public void test6() throws Exception{ //Create a freemark core configuration object and specify the freemarker version Configuration cf = new Configuration(()); //Specify the path to the template file (new File("F:\\template")); //Set template encoding ("utf-8"); //Get the template file object from the template file path Template template = (""); //Create map object and encapsulate template data Map<String,Object> maps = new HashMap<>(); ("today", new Date()); //Create the output object and output the data to the html page Writer out = new FileWriter(new File("F:\\template\\out\\")); //Generate html page (maps, out); //Close resources (); }
Introduce a page
When introducing another page to this page, you can use the following command to complete it.
Jsp Introduce Page:
Ftl introduction page: <#include "/include/">
Freemarker integration spring
Configure the integration of Freemarker spring configuration files:
<!-- freemarkerLeave it tospringmanage --> <!-- usespring提供模板manage配置对象 --> <bean class=""> <!-- Template path --> <property name="templateLoaderPath" value="/WEB-INF/fm/" /> <!-- Template encoding --> <property name="defaultEncoding" value="utf-8" /> </bean>
Create a template object
Freemarker Put into the server: Under the WEB-INF folder: To access the resource file, the server must be started.
In the spring configuration file loading application:
<!-- loadspringmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class></servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:,classpath:applicationContext-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Nginx access
Direct access cannot load style resources. You must go through the http server to load static resources.
At this time, nginx serves as the http server to access static resources.
Thank you for reading, I hope it can help you. Thank you for your support for this site!