Spring Boot Learning (d) Web Development rendering page--Freemarker__web

Source: Internet
Author: User

The Web Development Rendering page of Spring boot learning (d) was written in the previous chapter--Thymeleaf

Next, let's take a look at Freemarker integration Springboot

See controller:

    @RequestMapping ("/") public
    String index (Modelmap map) {
        map.addattribute ("name", "Xiao Jin gg");
        Return "index";
    

Pom.xml introduced:

<parent> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-parent</artifactid> <version>1.3.2.RELEASE</version> <relativePath/> <!--lookup parent from repository--> </parent> <properties> <project.build.sourceencoding>u Tf-8</project.build.sourceencoding> <java.version>1.8</java.version> </properties> < dependencies> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPR ing-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boo T</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> & lt;/dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>s

		Pring-boot-starter-web</artifactid> </dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-freemarker</artifactid> </dependency> </dependencies> <build> <plu gins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-m
			aven-plugin</artifactid> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>

Write a HELLO.FTL file that has a path under Src/main/resources/templates, where the contents of the HELLO.FTL file are as follows:

<! DOCTYPE html>

We can start our program for testing, access to the address:

Http://127.0.0.1:8080/hello, if you see the following message in your browser:

Freemarker template engine welcome Xiao into GG to freemarker!

Well, that means you're OK.

freemarker Configuration:

Some common configurations for Freemarker are provided in the Application.properties properties file of Spring boot, as follows:

Spring.freemarker.allow-request-override=false # Set Whether HttpServletRequest attributes are allowed to override (hide
) controller generated model attributes of the same name. Spring.freemarker.allow-session-override=false # Set Whether HttpSession attributes are allowed to override (hide) contro
Ller generated model attributes of the same name.
Spring.freemarker.cache=false # Enable Template caching.
Spring.freemarker.charset=utf-8 # Template encoding.
Spring.freemarker.check-template-location=true # Check that the templates location exists.
Spring.freemarker.content-type=text/html # Content-type value.
Spring.freemarker.enabled=true # Enable MVC View resolution for this technology. Spring.freemarker.expose-request-attributes=false # Set Whether all request attributes should is added to the model prior
To merging with the template. Spring.freemarker.expose-session-attributes=false # Set Whether all HttpSession attributes should is added to the model PR IOR to merging with the template. Spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a requestcontext for use by spring ' s macro libr
ary, under the name "Springmacrorequestcontext". Spring.freemarker.prefer-file-system-access=true # prefer file system access for template loading.
The File system access enables hot detection of template changes.
spring.freemarker.prefix= # prefix that gets prepended to view names when building a URL.
The spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # well-known Freemarker keys which'll is passed to Freemarker ' s Configuration.
spring.freemarker.suffix= # suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/# comma-separated List of template paths. spring.freemarker.view-names= # White List of view names so can be resolved.

Parameter default configuration, can write or write

Spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
Spring.freemarker.check-template-location=true
Spring.freemarker.charset=utf-8
spring.freemarker.content-type=text/html
Spring.freemarker.expose-request-attributes=false
Spring.freemarker.expose-session-attributes=false
Spring.freemarker.expose-spring-macro-helpers=false
#spring. freemarker.prefix=
#spring. freemarker.request-context-attribute=
#spring. freemarker.settings.* =
#spring. FREEMARKER.SUFFIX=.FTL
#spring. freemarker.template-loader-path=classpath:/templates/ comma-separated list
#spring. freemarker.view-names= # Whitelist of view names that can is resolved

To modify the Index method:

   @RequestMapping ("/") public
    String index (Modelmap map) {
        map.addattribute ("name", "Xiao Jin gg");
        Map.put ("Gender", 1);//gender: Sex, 1: male; 0: female;
        list<map<string,object>> friends =new <String,Object>> ();
        map<string,object> friend = new hashmap<string,object> ();
        Friend.put ("name", "John");
        Friend.put ("Age");
        Friends.add (friend);
        Friend = new hashmap<string,object> ();
        Friend.put ("name", "Dick");
        Friend.put ("Age");
        Friends.add (friend);
        Map.put ("Friends", friends);
        Return "index";
    

Modify Page

<! DOCTYPE html>

To restart a running project:

Http://127.0.0.1:8080/hello can see

Freemarker Template engine

Welcome Xiao into GG to freemarker!

Sex: Male

My friend:

Name: John, age 20
Name: Dick, age 22

At this point, Freemarker integration Springboot is complete!

Welcome everyone to Exchange learning Springboot,java and other fields of technology, Exchange Group: 587674051 of the source of the blog is also in the inside


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.