资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在SpringBoot中整合freemarker

这篇文章给大家介绍如何在SpringBoot中整合freemarker,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

我们提供的服务有:成都网站建设、网站制作、微信公众号开发、网站优化、网站认证、梁平ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的梁平网站制作公司

项目文件目录:

如何在SpringBoot中整合freemarker

首先,pom.xml中导入freemarker的依赖:


  org.springframework.boot
  spring-boot-starter-freemarker

application.properties(或yml)配置文件中加入freemarker相关配置:

#  freemarker静态资源配置
#    设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
#    关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

这里指定了freemarker文件的路径是classpath/templates,在resources文件夹下的templates新建freemarker文件夹,并且在其中新建index.ftl(上面配置文件中已经指定了freemarker模板的文件后缀为ftl):




  
  


FreeMarker模板引擎

${resource.name}

${resource.website}

${resource.language}

我们在resources下新建resource.properties:

com.haozz.opensource.name=wangshu
com.haozz.opensource.website=www.haozz.top:18158/
com.haozz.opensource.language=chinese

在SpringBoot启动类统计目录下新建utils包,在其中新建Resources类(此处使用配置文件引入相关数据):

package com.haozz.freemarkerdemo.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//表示这个类是一个读取配置文件的类
@Configuration
//指定配置的一些属性,其中的prefix表示前缀
@ConfigurationProperties(prefix = "com.haozz.opensource")
//指定所读取的配置文件的路径
@PropertySource(value = "classpath:resource.properties")
public class Resource {
  private String name;
  private String website;
  private String language;
  //...setter and getter
}

新建Controller包,新建FreeMarkerCtrl类:

package com.haozz.freemarkerdemo.controller;
import com.haozz.freemarkerdemo.utils.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/ftl")
public class FreeMarkerCtrl {
  @Autowired
  private Resource resource;
  @RequestMapping(value = "index")
  public String index(ModelMap map){
    map.addAttribute("resource",resource);
    return "freemarker/index";
  }
}

这里的ModelMap就相当于SpringMVC中的ModelAndView,其中的很多方法也很类似,我们这里返回的字符串就是freemarker模板的路径,不用写后缀,因为配置文件中已经指定了后缀为.ftl

浏览器发起请求,得到结果:

如何在SpringBoot中整合freemarker

这样,SpringBoot整合freemarker就好了。

我们再来试一下表格的形式。

FreeMarkerCtrl中新增方法:

@RequestMapping(value ="center")
  public String center(ModelMap map){
    map.put("users",parseUsers());
    map.put("title","用户列表");
    return "freemarker/center/center";
  }
  private List parseUsers(){
    List list= new ArrayList<>();
    for(int i=0;i<10;i++){
      Map map= new HashMap();
      map.put("name","kevin_"+i);
      map.put("age",10+i);
      map.put("phone","1860291105"+i);
      list.add(map);
    }
    return list;
  }

在resources/templates/freemarker下新建center文件夹,新建center.ftl:



  
  ${title}
  



  
    
    
    
  
    <#list users as user>
      
        
        
        
      
    
NameAgePhone
${user.name}${user.age}${user.phone}

浏览器请求:

如何在SpringBoot中整合freemarker

可以看到,在center.ftl中,我们使用了<#list users as user>的写法,这个相当于jstl表达式中的c:forEach。而users集合我们在FreeMarkerCtrl已经初始化了。

关于如何在SpringBoot中整合freemarker就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页名称:如何在SpringBoot中整合freemarker
转载来源:http://cdkjz.cn/article/pioghe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220