这篇文章将为大家详细讲解有关使用Springboot怎么对Freemarker进行整合,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1,导入依赖
& lt; dependency> & lt;才能groupId> org.springframework.boot</groupId> & lt;才能artifactId> spring-boot-starter-freemarker</artifactId> & lt;/dependency>
2,准备一个Freemarker模板(。ftl)
@ controller @RequestMapping (“/goodItem") public class  GoodItemController { ,@ reference private 才能;IGoodsService  goodsService; @ autowired才能 private 才能;Configuration 配置; @RequestMapping才能(“/createHtml") @ResponseBody才能 public 才能;String  createHtml (int gid, HttpServletRequest 请求){ ,,,//通过商品id获取商品详情信息 ,,,Goods Goods =, goodsService.queryById (gid); ,,,String [],图像=goods.getGimage () .split (“\ \ |“); ,,,//通过模板生成商品静态页面 ,,,try { ,,,,,//获取商品详情的模板对象 ,,,,,Template Template =, configuration.getTemplate (“goodsItem.ftl"); ,,,,,//准备商品数据 ,,,,,Map<字符串,Object>,地图=new HashMap<在(); ,,,,,map.put (“goods"、货物); ,,,,,map.put (“context", request.getContextPath ()); ,,,,,//freemarker页面没有分割功能,所以通过后台将图片分割后,将图片数组传到后台 ,,,,,map.put (“images"、图片); ,,,,,//生成静态页 ,,,,,//获得类路径路径 ,,,,,//静态页面的名称必须和商品有所关联,最简单的方式就是用商品的标识作为页面的名字 ,,,,,String path =, this.getClass () .getResource(“/静态/页面/?.getPath goods.getId () + () +“.html";; ,,,,,template.process(地图,new FileWriter(路径)); ,,,},catch (Exception e), { ,,,,,e.printStackTrace (); ,,,} ,,,return ““; ,,} }
注意:
1, freemarker页面不能通过& lt;基地th: href=https://www.yisu.com/zixun/? {# request.getContextPath () + '/'}“rel="外部nofollow”>获得项目的根路径。
因此可从后台将根路径传到前端,然后通过& lt;基地href=https://www.yisu.com/zixun/?{上下文}/眗el="外部nofollow”/>获取。
2,当页面是一个空文件夹的时候,会报的错。这是因为maven项目不会对空文件夹进行打包编译。
freemarker的基本语法
关于使用Springboot怎么对freemarker进行整合就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。