This period of work is to do a Web site navigation project, facing the user is a home page, so I think of using freemarker this template engine to static home page.
Before is implemented with JSP, in order to avoid the user every time Open page query database, so the use of JSP built-in object application, in the controller will query the data out,
Then put the application, and finally use the JSTL tag with the El expression to iterate through the data on the JSP page. This reduces the pressure on the server and the responsiveness of the page to a certain extent.
But still no static page response is fast.
To use the Freemarker step:
- Jar package, my project is built using MAVEN, so introducing the coordinates of the Freemarker jar package in Pom.xml.
- FTL template, I create a folder below Web-inf FTL, which only has the FTL template file, I created a INDEX.FTL file.
- The FTL template file is written in HTML tags and CSS styles, but the data section needs to be traversed using the tags provided by freemarker. As follows
<!--AD Suspension - <Divclass= "submenu"> <!--Tools - <Divclass= ' Xff '> <Divclass= "Slidetxtbox"> <Divclass= "HD"> <spanclass= "Arrow"><aclass= "Next"></a><aclass= "Prev"></a></span> <ul> <#listNewsmap?keys as TestKey> <Li>${testkey}</Li> </#list> </ul> </Div> <Divclass= "BD"style= "padding:5px 10px;"> <#listNewsmap?values as Value> <Divstyle= "text-align:left; table-layout:fixed; word-wrap:break-word; width:100%;"class= "Baidu"> <#listvalue as Newslist> <aTarget= "_blank"href= "${newslist.newsurl}"title= "${newslist.newsname}">${newslist.newsname}</a> </#list> </Div> </#list> </Div> </Div> </Div> </Div>
where < #list ></#list > is the traversal tag provided by Freemarker, Freemarker provides a lot of tags, which are not described here.
- The data is queried in Contorller, the data is fetched through the FTL template, and the complete data is then written to the HTML
//Get search engineList<searchengines> Searchengines = This. Indexservice.findsearchengines (); //get hot Search CustomersList<catalog> Hotsearchs = This. Indexservice.findhotsearchs (); //get Top 25 first level catalogsCatalogcustom custom =NewCatalogcustom (); Custom.setcataloglevel (1); List<Catalog> Toplevelcatalog = This. Indexservice.findcustomers (custom); //get top 10 customers in a first-level directorymap<string, list<catalog>> customermap =NewHashmap<string, list<catalog>>(); for(Catalog catalog:toplevelcatalog) {Catalogcustom customer=NewCatalogcustom (); Customer.setcataloglevel (3); Customer.setgfid (Catalog.getcatalogid ()); List<Catalog> customerlist = This. Indexservice.findcustomers (customer); Customermap.put (Catalog.getcatalogname (), customerlist); } //Get news-related datamap<string, list<news>> newsmap =NewHashmap<string, list<news>>(); List<NewsCatalog> newscatalogs = This. Indexservice.findnewscatalog (); for(Newscatalog newscatalog:newscatalogs) {News News=NewNews (); News.setpid (Newscatalog.getid ()); List<News> newslist = This. Indexservice.findnews (News); Newsmap.put (Newscatalog.getnewscatalog (), newslist); } //Get KeywordsList<keywords> Keywords = This. Indexservice.findkeywords (); /*Application.setattribute ("Newsmap", Newsmap); Application.setattribute ("Searchengines", searchengines); Application.setattribute ("Hotsearchs", Hotsearchs); Application.setattribute ("Customermap", Customermap); Application.setattribute ("keywords", keywords); */String Ftlpath= Session.getservletcontext (). Getrealpath ("/WEB-INF/FTL"); Configuration Configuration=NewConfiguration (); Configuration.setdirectoryfortemplateloading (NewFile (Ftlpath)); Configuration.setdefaultencoding ("UTF-8"); //Gets or creates a template. Template template = Configuration.gettemplate ("INDEX.FTL"); //get HTML static page fileString Indexpath = Session.getservletcontext (). Getrealpath ("/index.html"); //set the file input stream encoding, otherwise the generated HTML file will be garbled in ChineseFilewriterwithencoding out =NewFilewriterwithencoding (Indexpath, "UTF-8"); //put the data you want to show on the page in a maphashmap<string,object> map =NewHashmap<string, object>(); Map.put ("Newsmap", Newsmap); Map.put ("Searchengines", Searchengines); Map.put ("Hotsearchs", Hotsearchs); Map.put ("Customermap", Customermap); Map.put ("keywords", keywords); //The data in the map is entered into the INDEX.FTL template file and traversed, and the data of the entire template is then written to index.html. template.process (map, out); Out.close ();
Freemarker Generating HTML static pages