idea 使用spring boot 搭建freemarker模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37450089/article/details/85264729

省略创建springboot项目的步骤

配置pox.xml添加freemarker依赖

        <!--freemarker模板引擎-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>

等待依赖加载完毕

在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为后缀是为了区分其他模板语言

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
${name}
${id}
</body>
</html>

编写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

 

猜你喜欢

转载自blog.csdn.net/m0_37450089/article/details/85264729