003.Spring Boot Web Freemarker Sample

一、目录结构

003.Spring Boot Web Freemarker Sample_第1张图片
目录结构

二、pom.xml



    4.0.0

    com.airkisser
    spring-boot-sample-web-freemarker
    0.0.1-SNAPSHOT
    war

    spring-boot-sample-web-freemarker
    Spring boot web freemarker sample

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



三、application.properties

spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.ftl

spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.request-context-attribute=request
# freemarker settings
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.locale=zh_CN
spring.freemarker.settings.date_format=yyyy-MM-dd
spring.freemarker.settings.time_format=HH:mm:ss
spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
spring.freemarker.settings.number_format=0.##

# 是否开启Freemarker缓存,调试模式关闭
spring.freemarker.cache=false

四、Application.java

package com.airkisser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

五、HelloController.java

package com.airkisser;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;

@Controller
public class HelloController {

    @GetMapping("/")
    public String hello(Model model){
        model.addAttribute("date",new Date());
        model.addAttribute("message","Hello Freemarker.");
        return "welcome";
    }

}

六、welcome.ftl

<#-- @ftlvariable name="request" type="javax.servlet.http.HttpServletRequest" -->
<#-- @ftlvariable name="date" type="java.util.Date" -->
<#-- @ftlvariable name="message" type="java.lang.String" -->
<#assign contentPath=request.contextPath>



    
    Welcome


ContentPath: ${contentPath}

Date: ${date?datetime}
${message}

七、结果

ContentPath:
Date: 2017-03-05 22:32:11
Hello Freemarker. 

你可能感兴趣的:(003.Spring Boot Web Freemarker Sample)