笔记--Springboot集成Freemark

笔记--Springboot集成Freemark

  • 1.从spring官网下载一个springboot的包
    • 1.目录结构图
  • 2.引入freemark的pom依赖
  • 3.配置application.properties或application.yml(二选一)
  • 4.其他代码
    • 4.1 FreemarkerController代码
    • 4.2 SpringbootftlApplication代码
    • 4.3 WangzhanResourse代码
    • 4.4 wangzhan.properties
    • 4.5 center.ftl
    • 4.6 index.ftl
  • 打jar后启动效果预览

1.从spring官网下载一个springboot的包

1.目录结构图

笔记--Springboot集成Freemark_第1张图片

2.引入freemark的pom依赖



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.3.1.RELEASE
		 
	
	com.lifei
	springboot_ftl
	0.0.1-SNAPSHOT
	springboot_ftl
	Sringboot add freemarker  Demo project for Spring Boot

	
		1.8
		3.1.1
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		


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

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

		
			org.springframework.boot
			spring-boot-configuration-processor
			true
		
	

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



3.配置application.properties或application.yml(二选一)

application.properties

############################################################
# freemarker 静态资源配置
############################################################
#设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
#关闭缓存,即时刷新,上线生产需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

application.yml


#############################################################
## freemarker 静态资源配置
#############################################################
spring:
  freemarker:
     ##设定ftl文件路径
    template-loader-path: /templates
    ##关闭缓存,即时刷新,上线生产需要改为true
    cache: false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl

4.其他代码

4.1 FreemarkerController代码

package com.ftl.sp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ftl.sp.WangzhanResourse;

@Controller
@RequestMapping("ftl")
public class FreemarkerController {
     

	@Autowired
	private WangzhanResourse resourse;
	
	@RequestMapping("index")
	public String index(ModelMap map) {
     
		map.addAttribute("resource", resourse);
		return "freemarker/index";
	}
	
	@RequestMapping("center")
	public String center() {
     
		return "freemarker/center/center";
	}

}

4.2 SpringbootftlApplication代码

package com.ftl.sp;

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

@SpringBootApplication
public class SpringbootftlApplication {
     

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

}

4.3 WangzhanResourse代码

package com.ftl.sp;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix = "com.ftl.sp")
@PropertySource(value = "classpath:wangzhan.properties")
public class WangzhanResourse {
     

	private String name;

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

}

4.4 wangzhan.properties

com.ftl.sp.name = http://www.baidu.com
com.ftl.sp1 = http://www.baidu.com

4.5 center.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
FreeMarker模板引擎
<h1>center page</h1>
</body>
</html>

4.6 index.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
FreeMarker模板引擎
<h1>${resource.name}</h1>
</body>
</html>

打jar后启动效果预览

笔记--Springboot集成Freemark_第2张图片
笔记--Springboot集成Freemark_第3张图片

你可能感兴趣的:(springboot,java,java,springboot,freemarker)