SpringBoot集成freemaker

版权声明:原创,转载需告知 https://blog.csdn.net/qq_42136250/article/details/89642134

(一)pom.xml准备

<parent>
        <artifactId>springboot_parent</artifactId>
        <groupId>cn.lzj.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_04_webvelocity</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--导入模板依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.lzj.springboot.WebVelocityApplication</mainClass> <!--主类 包含main-->
                    <layout>JAR</layout>
                </configuration>
            </plugin>
        </plugins>
    </build>

(二)模板准备

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
             xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
    <body>
        <h1>我是Freemaker的模板, ${message}</h1>
    </body>
</html>

(三)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就可以实现通过模板,获取内容

猜你喜欢

转载自blog.csdn.net/qq_42136250/article/details/89642134
今日推荐