省略创建springboot项目的步骤
配置pox.xml添加freemarker依赖
org.springframework.boot
spring-boot-starter-freemarker
等待依赖加载完毕
在application.yml加入freemaker相关配置
server:
port: 8080
servlet:
context-path: /springboot-web
spring:
#FREEMARKER (FreeMarkerAutoConfiguration)
freemarker:
allow-request-override: false
#Enable template caching.启用模板缓存。
cache: false
check-template-location: true
charset: UTF-8
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#设置面板后缀
suffix: .ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
创建freemarker页面userInfo.ftl当然也可用直接用html,以ftl为后缀是为了区分其他模板语言
Title
${name}
${id}
编写FreemakerController
@Controller
@RequestMapping("/freemaker")
public class FreemakerController {
@RequestMapping("/showuser.html")
public ModelAndView showUsername(String id){
ModelAndView mv = new ModelAndView();
mv.addObject("id",id);
mv.addObject("name","dongguo");
mv.setViewName("/userInfo");
return mv;
}
}
访问页面
http://localhost:8080/springboot-web/freemaker/showuser.html?id=123