Spring Boot Uncover and Combat (vii) practical technology-Freemarker template engine

Source: Internet
Author: User

Article Directory

    1. 1. Freemaker instead of JSP as a page render
    2. 2. Generating static files
    3. 3. Extended Reading
    4. 4. Source Code

Spring Boot provides support for a number of template engines, such as Freemarker, Thymeleaf. In this article, we look at how Spring Boot integrates and uses Freemarker.

Using the Freemarker template in Spring Boot is easy and easy. If you want to use the Freemarker template engine, first, modify the POM file to add dependencies.

Freemaker instead of JSP as a page render
    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-freemarker</artifactId>
    4. </dependency>

Then we create the template. It is worth noting that the Spring Boot integrated freemarker default configuration file is placed in classpath:/templates/. Therefore, we need to add the template file in src/main/resources/templates/.

For example, we add a template file called WELCOME.FTL.

    1. <! DOCTYPE html>
    2. lang= "en" >
    3. Date: ${time? Date}<BR>
    4. Message: ${message}
    5. </BODY>
    6. </ Html>

So, the last step, we just need to configure this in the control class.

  1. @Controller("Template.freemarkercontroller")
  2. Public class welcomecontroller {
  3. @RequestMapping("/template/freemarker/welcome")
  4. public String Welcome(Map<string, Object> model ) {
  5. Model. Put("Time", new Date());
  6. Model. Put("message", "Liang");
  7. return "Welcome";
  8. }
  9. }

Remember our previous webmain, let's look back.

  1. @RestController
  2. @EnableAutoConfiguration
  3. @ComponentScan(basepackages = { "com.lianggzone.springboot" })
  4. Public class webmain {
  5. public static void main(String[] args) throws Exception {
  6. springapplication. Run(webmain. Class, args);
  7. }
  8. }

Run the Webmain class directly, or you can launch the app from the command line with "MVN Spring-boot:run". An embedded TOMCAT server will start running on port 8080. Access "Http://localhost:8080/template/freemarker/welcome" to see the results displayed on the page.

Generating static files

The above scenario is a very typical MVC usage scenario, and we use Freemaker instead of JSP as a page rendering. However, with the separation of the front and back, JSP gradually away from our vision, the service side more processing business logic, through RESTful or RPC external services. Page interaction, to the front-end to do rendering.

In this case, is not freemarker to be useless? In fact, freemarker as a template engine, there are a lot of usage scenarios, for example, we can separate, we can move the relatively unchanged content through the Freemarker rendering generated static files uploaded to the content service, content services through the CDN for resource distribution.

So, let's make a small makeover of the above code to simulate a file generation to a local scene.

  1. @RestController("Template.freemarkercontroller2")
  2. @EnableAutoConfiguration
  3. Public class welcome2controller {
  4. @Autowired
  5. private configuration configuration;
  6. @RequestMapping("/template/freemarker/welcome2")
  7. public String welcome2(map<string, object= "" > model) throws Exception {
  8. <string, object= "" > model. Put("Time", new Date());
  9. <string, object= "" > model. Put("message", "Liang");
  10. <string, object= "" >
  11. <string, object= "" > template template = configuration. GetTemplate("WELCOME.FTL");
  12. <string, object= "" > string content = freemarkertemplateutils. Processtemplateintostring(template, model);
  13. <string, object= "" >
  14. <string, object= "" > FileUtils. Writestringtofile(new File("d:/welcome.html"), content);
  15. <string, object= "" >
  16. <string, object= "" > return "Welcome";
  17. <string, object= "" > }
  18. <string, object= "" >}

Run the Webmain class directly, Access "http://localhost:8080/template/freemarker/welcome2" to see the results on the page, and view the D drive, whether the file is generated?

Extended Reading
    • Freemarker Generating HTML templates
Source Code

Related example complete code: springboot-action
Static page Builder: freemarker-utils

Finish



If you feel that my article is helpful to you, please feel free to make a reward.

    • Copyright NOTICE: This article was published by Liang in Liang Blog
    • Reprint statement: Free reprint-Non-commercial-non-derivative-maintain attribution (Creative Sharing 3.0 license), non-commercial reprint please indicate the author and source, commercial reprint please contact the author himself.
    • Article title: Spring Boot Secret and actual combat (vii) practical technology-Freemarker template engine
    • Article Link: http://blog.720ui.com/2017/springboot_07_othercore_freemarker/

Spring Boot Uncover and Combat (vii) practical technology-Freemarker template engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.