亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程

 更新時(shí)間:2020年12月20日 11:50:26   作者:sinJack  
這篇文章主要介紹了Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

基本配置、測(cè)試

1、導(dǎo)入依賴(lài)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、準(zhǔn)備一個(gè)Freemarker模板(.ftl)

在這里插入圖片描述

3、注入Configuration對(duì)象(freemarker.template包下)

在這里插入圖片描述

4、生成商品詳情模板

@Controller
@RequestMapping("/goodItem")
public class GoodItemController {
  @Reference
  private IGoodsService goodsService;

  @Autowired
  private Configuration configuration;

  @RequestMapping("/createHtml")
  @ResponseBody
  public String createHtml(int gid, HttpServletRequest request){
    //通過(guò)商品id獲取商品詳情信息
    Goods goods = goodsService.queryById(gid);
    String [] images=goods.getGimage().split("\\|");
    //通過(guò)模板生成商品靜態(tài)頁(yè)面
    try {
      //獲取商品詳情的模板對(duì)象
      Template template = configuration.getTemplate("goodsItem.ftl");
      //準(zhǔn)備商品數(shù)據(jù)
      Map<String,Object> map=new HashMap<>();
      map.put("goods",goods);
      map.put("context",request.getContextPath());
      //freemarker頁(yè)面沒(méi)有分割功能,所以通過(guò)后臺(tái)將圖片分割后,將圖片數(shù)組傳到后臺(tái)
      map.put("images",images);
      //生成靜態(tài)頁(yè)
      //獲得classpath路徑
      //靜態(tài)頁(yè)面的名稱(chēng)必須和商品有所關(guān)聯(lián),最簡(jiǎn)單的方式就是用商品的id作為頁(yè)面的名字
      String path = this.getClass().getResource("/static/page/").getPath()+goods.getId()+".html";;
      template.process(map,new FileWriter(path));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
}

注意:
1、freemarker頁(yè)面不能通過(guò)<base th:href="${#request.getContextPath()+'/'}" rel="external nofollow" >獲得項(xiàng)目的根路徑。
因此可從后臺(tái)將根路徑傳到前端,然后通過(guò)<base href="${context}/" rel="external nofollow" />獲取。
2、當(dāng)page是一個(gè)空文件夾的時(shí)候,會(huì)報(bào)錯(cuò)。這是因?yàn)閙aven項(xiàng)目不會(huì)對(duì)空文件夾進(jìn)行打包編譯。

FreeMarker的基本語(yǔ)法

在這里插入圖片描述

在這里插入圖片描述

到此這篇關(guān)于Springboot整合Freemarker的實(shí)現(xiàn)詳細(xì)過(guò)程的文章就介紹到這了,更多相關(guān)Springboot整合Freemarker內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論