Springboot整合Freemarker的实现详细过程

作者:sinJack 时间:2023-01-23 22:09:57 

基本配置、测试

1、导入依赖


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

2、准备一个Freemarker模板(.ftl)

Springboot整合Freemarker的实现详细过程

3、注入Configuration对象(freemarker.template包下)

Springboot整合Freemarker的实现详细过程

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){
   //通过商品id获取商品详情信息
   Goods goods = goodsService.queryById(gid);
   String [] images=goods.getGimage().split("\\|");
   //通过模板生成商品静态页面
   try {
     //获取商品详情的模板对象
     Template template = configuration.getTemplate("goodsItem.ftl");
     //准备商品数据
     Map<String,Object> map=new HashMap<>();
     map.put("goods",goods);
     map.put("context",request.getContextPath());
     //freemarker页面没有分割功能,所以通过后台将图片分割后,将图片数组传到后台
     map.put("images",images);
     //生成静态页
     //获得classpath路径
     //静态页面的名称必须和商品有所关联,最简单的方式就是用商品的id作为页面的名字
     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页面不能通过<base th:href="${#request.getContextPath()+'/'}" rel="external nofollow" >获得项目的根路径。
因此可从后台将根路径传到前端,然后通过<base href="${context}/" rel="external nofollow" />获取。
2、当page是一个空文件夹的时候,会报错。这是因为maven项目不会对空文件夹进行打包编译。

FreeMarker的基本语法

Springboot整合Freemarker的实现详细过程

Springboot整合Freemarker的实现详细过程

来源:https://blog.csdn.net/qq_40693603/article/details/107815198

标签:Springboot,整合,Freemarker
0
投稿

猜你喜欢

  • Android监听键盘状态获取键盘高度的实现方法

    2023-12-02 16:44:17
  • 搭建一个基础的Resty项目框架

    2021-06-08 11:12:06
  • SpringBoot整合Shiro实现登录认证的方法

    2022-03-23 01:12:19
  • Android应用中使用ViewPager和ViewPager指示器来制作Tab标签

    2021-08-04 11:33:37
  • 解析:ClickOnce通过URL传递参数 XXX.application?a=1

    2022-05-26 01:54:34
  • C#中static void Main(string[] args) 参数示例详解

    2023-10-06 04:44:01
  • Mybatis-plus自动填充不生效或自动填充数据为null原因及解决方案

    2021-08-16 02:18:42
  • Spring JPA学习之delete方法示例详解

    2021-11-23 12:22:55
  • SpringBoot Actuator未授权访问漏洞修复详解

    2022-03-30 16:43:28
  • WinForm生成验证码图片的方法

    2022-05-11 17:49:33
  • Eclipse的Debug调试技巧大全(总结)

    2023-11-25 06:14:06
  • C#检测DataSet是否为空的方法

    2023-02-24 03:23:41
  • Android ADB常用命令总结

    2022-05-24 18:07:42
  • Java读取txt文件和写入txt文件的简单实例

    2022-01-01 05:04:16
  • 一文详解Jetpack Android新一代导航管理Navigation

    2022-12-08 20:40:14
  • Android 中的两端对齐实例详解

    2022-05-13 06:58:18
  • C#中DataTable和List互转的示例代码

    2022-08-25 21:40:06
  • kafka消费者kafka-console-consumer接收不到数据的解决

    2022-04-26 06:05:42
  • 详解Android Service与Activity之间通信的几种方式

    2023-10-26 04:15:48
  • 解决idea每次新建项目都需要重新指定maven目录

    2022-12-25 17:39:52
  • asp之家 软件编程 m.aspxhome.com