spring boot 项目集成 freemarker时,未进行正确配置的话会出现404错误,如图所示:

spring boot 要集成 freemarker 模板引擎时必须经过正确的配置,大致可分为5 个步骤:
引入依赖
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-freemarker</artifactid>
</dependency>在application.yml文件中进行如下配置,务必配置正确(.properties文件也一样)
freemarker:
template-loader-path: classpath:/templates # classpath: 一定不能漏写
cache: false
charset: utf-8
check-template-location: true
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
request-context-attribute: req
suffix: .ftl曾经因为漏写 classpath: ,花了1个小时找不到原因
编写controller类
@controller
@requestmapping("/")
public class ordercontroller {
@getmapping("/list")
public modelandview list(map<string, object> map) {
map.put("name", "chenf24k");
return new modelandview("name", map);
}
}在spring boot项目的resources/templates下新建 name.ftl模板文件
<h1>freemarker</h1>
<h2>${name}</h2>启动spring boot 项目后浏览器输入地址: http://127.0.0.1:8080/list 进行访问。

到此这篇关于spring boot集成freemarker 时访问不到.ftl文件的配置步骤的文章就介绍到这了,更多相关spring boot访问不到.ftl文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论