目录

最新版本WebContext构造函数-避坑

目录

最新版本WebContext构造函数-避坑

import org.thymeleaf.context.IWebContext;
import org.thymeleaf.context.WebContext;

当你想把页面信息全部获取出来存到redis缓存中使用时,SpringWebContext在Spring5中报错

       SpringWebContext ctx = new SpringWebContext(request, response,
                request.getServletContext(), request.getLocale(), model.asMap(), applicationContext);

代码报错1,很早版本之前的解决办法如下(SpringWebContext在spring5中过时)

  //手动渲染
        IWebContext ctx =new WebContext(request,response,
                request.getServletContext(),request.getLocale(),model.asMap());

但是最新版本仍然报错

https://i-blog.csdnimg.cn/direct/56edfae3370f4492808e72833deea171.png

根据WebContext提示

https://i-blog.csdnimg.cn/direct/7afedd34ea0c4f48bf907bafd644fcf6.png

解决办法如下

        IWebContext ctx =new WebContext(
                (IWebExchange) request.getServletContext(),request.getLocale(),Map.of("user",user,"itemList",itemList));