• SpringBoot 手动渲染Thymeleaf模版工具类


            为了解决在模版中通过 @bean.method() 方式调用 spring 的bean对象方法报错问题,参考ThymeleafView的rendFragment()方法, 删减了一些用不到的代码,记录一下备用.

    1. import org.springframework.context.ApplicationContext;
    2. import org.springframework.core.convert.ConversionService;
    3. import org.springframework.web.servlet.support.RequestContext;
    4. import org.thymeleaf.IEngineConfiguration;
    5. import org.thymeleaf.context.WebExpressionContext;
    6. import org.thymeleaf.spring5.ISpringTemplateEngine;
    7. import org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext;
    8. import org.thymeleaf.spring5.expression.ThymeleafEvaluationContext;
    9. import org.thymeleaf.util.FastStringWriter;
    10. import javax.servlet.ServletContext;
    11. import javax.servlet.http.HttpServletRequest;
    12. import javax.servlet.http.HttpServletResponse;
    13. import java.io.Writer;
    14. import java.util.HashMap;
    15. import java.util.Locale;
    16. import java.util.Map;
    17. /**
    18. * 结合springMVC使用的thymeleaf 模版手动渲染,解决模版页面 @xxx 调用springbean方法报错问题
    19. */
    20. public class ThymeleafUtil {
    21. public static String renderTemplate(String templateName, Map modelMap,
    22. HttpServletRequest request, HttpServletResponse response,
    23. ApplicationContext applicationContext,
    24. ISpringTemplateEngine viewTemplateEngine) throws Exception {
    25. ServletContext servletContext = request.getServletContext();
    26. Map mergedModel = new HashMap(30);
    27. if (modelMap != null) {
    28. mergedModel.putAll(modelMap);
    29. }
    30. RequestContext requestContext = new RequestContext(request, response, servletContext, mergedModel);
    31. SpringWebMvcThymeleafRequestContext thymeleafRequestContext = new SpringWebMvcThymeleafRequestContext(requestContext, request);
    32. if (!mergedModel.containsKey("springRequestContext")) {
    33. mergedModel.put("springRequestContext", requestContext);
    34. }
    35. if (!mergedModel.containsKey("springMacroRequestContext")) {
    36. mergedModel.put("springMacroRequestContext", requestContext);
    37. }
    38. mergedModel.put("thymeleafRequestContext", thymeleafRequestContext);
    39. ConversionService conversionService = (ConversionService) request.getAttribute(ConversionService.class.getName());
    40. ThymeleafEvaluationContext evaluationContext = new ThymeleafEvaluationContext(applicationContext, conversionService);
    41. mergedModel.put("thymeleaf::EvaluationContext", evaluationContext);
    42. IEngineConfiguration configuration = viewTemplateEngine.getConfiguration();
    43. WebExpressionContext context = new WebExpressionContext(configuration, request, response, servletContext, Locale.getDefault(), mergedModel);
    44. Writer templateWriter = new FastStringWriter(1024);
    45. viewTemplateEngine.process(templateName, null, context, (Writer) templateWriter);
    46. return templateWriter.toString();
    47. }
    48. }

  • 相关阅读:
    Windows10/11 缩放与布局自定义
    java计算机毕业设计考勤系统设计源码+mysql数据库+系统+lw文档+部署
    【测试工具】Fiddler
    Android JetPack~LiveData(二) 数据倒灌问题
    Sui主网升级至V1.11.2版本
    Exam Results (尺取)
    Springboot 异步Async教程
    shell脚本之数组元素排序
    Bug战场:C++篇
    集成电路技术——如何制造芯片(1)
  • 原文地址:https://blog.csdn.net/fmi110/article/details/126156884