Spring-boot框架下的Maven项目使用freemarker模板引擎

 

freemarker我们之前就使用过很多次

在SpringBoot中我们想要使用freemarker模板引擎那么我们需要以下步骤(这里讲解maven项目中的整合)

1.首先我们需要在pom文件中引入依赖

<!-- 引入freemarker模板引擎依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
			<scope>true</scope>
		</dependency>

2.接着我们需要在配置application.properties文件中进行配置freemarker文件

#springBoot模板引擎相关配置
########################################
#设定ftl模板文件路径
spring.freemarker.template-loader-path=classpath:/templates
#设定模板编码格式
spring.freemarker.charset=UTF-8
#关闭缓存及时刷新,上线产品需要改为true
spring.freemarker.cache=false
#设置文本模板类型
spring.freemarker.content-type=text/html;
#设置ftl模板文件后缀
spring.freemarker.suffix=.ftl
#检查模板位置是否存在
spring.freemarker.check-template-location=true
#设置是否应该在与模板合并之前将所有请求属性添加到模型中
spring.freemarker.expose-request-attributes=true
#设置是否应该在与模板合并之前将所有HttpSession属性添加到模型中
spring.freemarker.expose-session-attributes=true
#配置所有视图的上下文请求属性的名称。
spring.freemarker.request-context-attribute=request

3.之后我们只需要在控制层里面编写需求代码就可以了

@Controller
@RequestMapping("ftl")
public class FreemarkerController{
	@Autowired
	Resource resource;
		
	@RequestMapping("/index")
	public String index(ModelMap map) {
			
		map.addAttribute("resource", resource);
		return "freemarker/index";
	}
}

页面的代码如下 : 

freemarker模板引擎
	<h1>${resource.name}</h1>
	<h1>${resource.website}</h1>
	<h1>${resource.language}</h1>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值