• springboot+thymeleaf 封装自定义解析json字符串标签方法


    一、首先是解析json字符串的方法类

    一共四个方法,用于转换json或json数组,或者直接从json或json数组中取值。

    1. package com.gnetek.ws.core.thymeleaf;
    2. import cn.hutool.core.util.StrUtil;
    3. import cn.hutool.json.JSONArray;
    4. import cn.hutool.json.JSONObject;
    5. import cn.hutool.json.JSONUtil;
    6. import java.util.List;
    7. /**
    8. * thymeleaf模板自定义 json 工具方法
    9. *
    10. * @author mrchh
    11. * @date 2023/11/16
    12. */
    13. public class ThJson {
    14. /**
    15. * 从jsonStr中获取key的值
    16. *
    17. * @param jsonStr json str
    18. * @param key 关键
    19. * @return {@link Object}
    20. */
    21. public Object getStrJson(String jsonStr, String key) {
    22. Object obj = null;
    23. if (StrUtil.isNotBlank(jsonStr) && StrUtil.isNotBlank(key)) {
    24. JSONObject json = JSONUtil.parseObj(jsonStr);
    25. obj = json.get(key);
    26. }
    27. return obj;
    28. }
    29. /**
    30. * 从jsonArray数组中获取第i个的值
    31. *
    32. * @param arrayStr 数组str
    33. * @param i 我
    34. * @return {@link Object}
    35. */
    36. public Object getStrArray(String arrayStr, int i) {
    37. Object obj = null;
    38. if (StrUtil.isNotBlank(arrayStr) && i>=0) {
    39. JSONArray jsonArray = JSONUtil.parseArray(arrayStr);
    40. if (i<jsonArray.size()) {
    41. obj = jsonArray.get(i);
    42. }
    43. }
    44. return obj;
    45. }
    46. /**
    47. * json数组转成数组
    48. *
    49. * @param arrayStr 数组str
    50. * @return {@link List}<{@link Object}>
    51. */
    52. public Object[] parseJsonArray(String arrayStr) {
    53. Object[] array = null;
    54. if (StrUtil.isNotBlank(arrayStr)) {
    55. JSONArray jsonArray = JSONUtil.parseArray(arrayStr);
    56. array = jsonArray.toArray();
    57. }
    58. return array;
    59. }
    60. /**
    61. * json字符串转成 JSONObject
    62. *
    63. * @param jsonStr json str
    64. * @return {@link JSONObject}
    65. */
    66. public JSONObject parseJson(String jsonStr) {
    67. JSONObject json = null;
    68. if (StrUtil.isNotBlank(jsonStr)) {
    69. json = JSONUtil.parseObj(jsonStr);
    70. }
    71. return json;
    72. }
    73. }

    二、接下来是表达式配置工厂类,以后添加别的方法也可以用,不用再写一个。

    1. package com.gnetek.ws.core.thymeleaf;
    2. import org.thymeleaf.context.IExpressionContext;
    3. import org.thymeleaf.expression.IExpressionObjectFactory;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6. import java.util.Set;
    7. /**
    8. * 表达式配置工厂
    9. *
    10. * @author mrchh
    11. * @date 2023/11/16
    12. */
    13. public class ThIExpressionObjectFactory implements IExpressionObjectFactory {
    14. private Map<String, Object> map;
    15. public ThIExpressionObjectFactory(Map<String, Object> map) {
    16. this.map = new HashMap<>(map);
    17. }
    18. /**
    19. * 获取所有表达式对象名称
    20. *
    21. * @return {@link Set}<{@link String}>
    22. */
    23. @Override
    24. public Set<String> getAllExpressionObjectNames() {
    25. return map.keySet();
    26. }
    27. /**
    28. * 根据表达式名字获取表达式对象
    29. *
    30. * @param iExpressionContext 表达语境
    31. * @param s 名称
    32. * @return {@link Object}
    33. */
    34. @Override
    35. public Object buildObject(IExpressionContext iExpressionContext, String s) {
    36. return map.get(s);
    37. }
    38. /**
    39. * 是否允许缓存
    40. *
    41. * @param s 名称
    42. * @return boolean
    43. */
    44. @Override
    45. public boolean isCacheable(String s) {
    46. return true;
    47. }
    48. }

    三、将表达式工厂添加到springboot配置

    1. package com.gnetek.ws.core.thymeleaf;
    2. import org.springframework.stereotype.Component;
    3. import org.thymeleaf.dialect.AbstractDialect;
    4. import org.thymeleaf.dialect.IExpressionObjectDialect;
    5. import org.thymeleaf.expression.IExpressionObjectFactory;
    6. import java.util.HashMap;
    7. import java.util.Map;
    8. @Component
    9. public class ThIExpressionFactoryDialect extends AbstractDialect implements IExpressionObjectDialect {
    10. private final IExpressionObjectFactory iExpressionObjectFactory;
    11. protected ThIExpressionFactoryDialect() {
    12. super("DarrenExpression");
    13. //map中可以多个
    14. Map<String, Object> map = new HashMap<>();
    15. //配置我们刚才写的工具类,key为页面上引用的名字
    16. map.put("thJson", new ThJson());
    17. iExpressionObjectFactory = new ThIExpressionObjectFactory(map);
    18. }
    19. @Override
    20. public IExpressionObjectFactory getExpressionObjectFactory() {
    21. return iExpressionObjectFactory;
    22. }
    23. }

    四、在页面中使用自定义的方法

    th:alt="${#thJson.parseJson(lang.langName).zh}"
    [[${#thJson.getStrJson(lang.langName, lang.langAbb)}]]
    th:each="ins : ${#thJson.parseJsonArray(session.gneCompany.aboutUsIns)}"
    [[${#thJson.getStrArray(session.gneCompany.companySlogan, 1)}]]

  • 相关阅读:
    QGIS编译(跨平台编译)之五十一:Qt Creator环境下qgis_native库的pro文件
    Hadoop内hive之间,hive与DB、ES等之间数据交互的问题与解决方案
    Vue3 - 全局指令(详细教程)
    WPF 做一个超级简单的 1024 数字接龙游戏
    Win11怎么显示固定应用?
    计算机网络入门基础篇——应用层
    05-jQuery的ajax
    PCB 二:AD 原理图绘制以及PCB绘制
    Python运算符 成员运算符、身份运算符,三目运算符
    Java 线程池之任务拒绝策略
  • 原文地址:https://blog.csdn.net/ihchenchen/article/details/134449471