In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "SpringBoot2 how to use the FreeMarker template to complete the page static processing", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to use the FreeMarker template in SpringBoot2 to complete page static processing" it!
Static pages 1. Dynamic and static pages
Static page
That is, a static web page, which means that the content HTML page has been loaded and displayed on the client browser without going through the request for server data and compilation process. Generally speaking, it is to generate independent HTML pages without data interaction with the server.
Description of advantages and disadvantages:
The content of the static web page is stable, and the page loading speed is very fast.
Do not interact with the server to improve security
The interaction of static web pages is poor, and the real-time data is very low.
Dimension costs are high, generating a lot of HTML pages
Dynamic page
It refers to a web page programming technology relative to a static web page, in which the content of the page needs to be obtained by the server. Without considering the cache, the data of the service interface changes, and the content loaded on the page will change in real time. However, the displayed content changes dynamically with the result of database operation.
Description of advantages and disadvantages:
Real-time acquisition of data from dynamic web pages with low delay
Relying on database interaction, the cost of page maintenance is very low
The cost of security control is high when interacting with database in real time.
Page loading speed is very dependent on the performance of databases and services.
Dynamic pages and static pages have a strong relativity and are easier to understand by comparison.
2. Application scenarios
There are many application scenarios for static processing of dynamic pages, such as:
The head and bottom of a large website are loaded uniformly after static.
Media website, the content is rendered and directly converted to HTML pages
Static web pages of CDN edge node agent under high concurrency
Complex product details page processing in e-commerce website
The foundation of static technology: prompt the response speed of the service, or make the response node in advance, such as general process, page (client) request service, service processing, response data, page loading, a series of processes are not only complex, but also time-consuming, if based on static technology processing, directly load the static page, the request ends.
Second, process analysis
Static page conversion is a relatively complex process, and the core process is as follows:
Develop a page template, that is, static page style
Provide an interface to obtain data for the page template
The parsing process of writing data interface return parameters in the page template
Merge data and page template based on parsing engine
After the page template content is loaded, it is converted to a HTML static page
Upload HTML static pages to the file server
The client (Client) gets the url load display of the static page
The main process is roughly like the above, and if the data interface response parameters change, the static page needs to be regenerated, so the real-time performance of data loading will be much lower.
Third, code implementation case 1, basic dependence
FreeMarker is a template engine: a general tool based on templates and data to be changed and used to generate output text (HTML pages, e-mails, configuration files, source code, etc.).
Org.springframework.boot spring-boot-starter-freemarker2, page template
The template style developed by FreeMarker is used here.
PageStatic topic: ${myTitle} author: ${data.auth} date: ${data.date} Specification Product details ${info.desc}
The syntax of FreeMarker is basically the same as the original HTML syntax, but it has its own set of data processing tags, which is not complicated to use.
3. Parsing process
Through parsing, you can merge the data of the page template and the data interface together.
@ Servicepublic class PageServiceImpl implements PageService {private static final Logger LOGGER = LoggerFactory.getLogger (PageServiceImpl.class); private static final String PATH = "/ templates/"; @ Override public void ftlToHtml () throws Exception {/ / create configuration class Configuration configuration = new Configuration (Configuration.getVersion ()); / / set template path String classpath = this.getClass (). GetResource ("/"). GetPath () Configuration.setDirectoryForTemplateLoading (new File (classpath + PATH)); / / load template Template template = configuration.getTemplate ("my-page.ftl"); / / data model Map map = new HashMap (); map.put ("myTitle", "page static (PageStatic)"); map.put ("tableList", getList ()); map.put ("imgList", getImgList ()) / / static page content String content = FreeMarkerTemplateUtils.processTemplateIntoString (template, map); LOGGER.info ("content: {}", content); InputStream inputStream = IOUtils.toInputStream (content, "UTF-8"); / / output file FileOutputStream fileOutputStream = new FileOutputStream (new File ("F:/page/newPage.html")); IOUtils.copy (inputStream, fileOutputStream) / / close stream inputStream.close (); fileOutputStream.close ();} private List getList () {List tableInfoList = new ArrayList (); tableInfoList.add (new TableInfo (Constant.desc1, Constant.img01)); tableInfoList.add (new TableInfo (Constant.desc2,Constant.img02)); return tableInfoList;} private List getImgList () {List imgList = new ArrayList () ImgList.add (Constant.img02); imgList.add (Constant.img02); return imgList }} Thank you for your reading, the above is the content of "how to use the FreeMarker template to complete the static processing of the page in SpringBoot2". After the study of this article, I believe you have a deeper understanding of how to use the FreeMarker template to complete the static processing of the page in SpringBoot2. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.