idea 使用spring boot 搭建freemarker模板


 

一丶新建maven spring boot 項目

 

新建好了開始使用模板

先看一個目錄結構

 

二丶配置pox.xml

 

<?xml version="1.0" encoding="UTF-8"?>
 <project  xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId> <!--引入依賴 這里特別重要-->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

引入依賴后 

點項目后右鍵 

等待加載完畢

三丶編寫helloController

package com.example.springbootdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
//如果你不想使用模板,請使用ResControllre 這個是不渲染模板的
public class HelloController { @RequestMapping(value = "/index") public String hello(Map<String,Object> map){ map.put("hello","Hello FreeMarker"); map.put("message","這是一條信息"); return "hello"; } }

編寫 application.properties

#選擇自己喜歡的端口,這個隨意
server.port=8000
########################################################

###FREEMARKER (FreeMarkerAutoConfiguration)

########################################################

spring.freemarker.allow-request-override=false
#Enable template caching.啟用模板緩存。
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=.html

#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list

#spring.freemarker.view-names= # whitelist of view names that can be resolved

編寫html

<!DOCTYPE html>
<html>
<body>
<h4>親愛的${hello},你好!</h4>
<p style="color:blue;">${message}</p>
祝:開心!
</br>
</body>
</html>  

運行....

 

 更多模板教程請查看官方文檔 http://freemarker.foofun.cn/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM