Freemarker是三大主流模版引擎之一,与Jsp,Velocity并称“三剑客“。
①在pom.xml中添加依赖
<!-- freemarker 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>②freemarker在application.properities的配置
# FREEMARKER (FreeMarkerAutoConfiguration) spring.freemarker.allow-request-override=false spring.freemarker.allow-session-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.enabled=true spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=true spring.freemarker.prefer-file-system-access=true spring.freemarker.suffix=.html spring.freemarker.template-loader-path=classpath:/ftl/ spring.freemarker.settings.template_update_delay=0 spring.freemarker.settings.default_encoding=UTF-8 spring.freemarker.settings.classic_compatible=true spring.freemarker.order=1
当然这里的访问资源的后缀也可以是.ftl,这是正统的freemarker
spring.freemarker.suffix=.ftl这里笔者对html掌握得较好,所以优先选择html;
③根据路径配置在resources下创建ftl目录,并新建test.html
spring.freemarker.template-loader-path=classpath:/ftl/
test.html代码如下:
<!DOCTYPE> <html> <head> <title> freemarker </title> </head> <body> <h1>Hello ${name} from resource freemark!</h1> </body>④编辑测试controller
@RequestMapping("ftl/test") public String testFtl(ModelMap model){ model.addAttribute("name","FreeMarker 模版引擎 "); return "test"; }注意最外层的控制层注解只能@用@Controller;
⑤访问测试
好了,我是张星,欢迎加入博主技术交流群,群号:313145288