SpringBoot集成freemaker

(一)pom.xml准备


        springboot_parent
        cn.lzj.springboot
        1.0-SNAPSHOT
    
    4.0.0

    springboot_04_webvelocity
    jar

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

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    utf-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    cn.lzj.springboot.WebVelocityApplication 
                    JAR
                
            
        
    

(二)模板准备




    Hello World!

    
        

我是Freemaker的模板, ${message}

(三)controller

package cn.lzj.springboot.controller;

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

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("message", "沐子");
        return "index";
    }

    @RequestMapping("/index1")
    @ResponseBody
    public String index1(Model model){
        model.addAttribute("message", "希子");
        return "index";
    }
}

(四)application.properties

# FreeeMarker 模板引擎配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
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 be resolved

(五)SpringBoot的应用器

package cn.lzj.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebVelocityApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebVelocityApplication.class);
    }
}

到此,通过主方法,启动SpringBoot就可以实现通过模板,获取内容

你可能感兴趣的:(框架)