一共四个方法,用于转换json或json数组,或者直接从json或json数组中取值。
- package com.gnetek.ws.core.thymeleaf;
-
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.json.JSONArray;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
-
- import java.util.List;
-
- /**
- * thymeleaf模板自定义 json 工具方法
- *
- * @author mrchh
- * @date 2023/11/16
- */
- public class ThJson {
-
- /**
- * 从jsonStr中获取key的值
- *
- * @param jsonStr json str
- * @param key 关键
- * @return {@link Object}
- */
- public Object getStrJson(String jsonStr, String key) {
- Object obj = null;
-
- if (StrUtil.isNotBlank(jsonStr) && StrUtil.isNotBlank(key)) {
- JSONObject json = JSONUtil.parseObj(jsonStr);
- obj = json.get(key);
- }
-
- return obj;
- }
-
- /**
- * 从jsonArray数组中获取第i个的值
- *
- * @param arrayStr 数组str
- * @param i 我
- * @return {@link Object}
- */
- public Object getStrArray(String arrayStr, int i) {
- Object obj = null;
-
- if (StrUtil.isNotBlank(arrayStr) && i>=0) {
- JSONArray jsonArray = JSONUtil.parseArray(arrayStr);
- if (i<jsonArray.size()) {
- obj = jsonArray.get(i);
- }
- }
-
- return obj;
- }
-
- /**
- * json数组转成数组
- *
- * @param arrayStr 数组str
- * @return {@link List}<{@link Object}>
- */
- public Object[] parseJsonArray(String arrayStr) {
- Object[] array = null;
-
- if (StrUtil.isNotBlank(arrayStr)) {
- JSONArray jsonArray = JSONUtil.parseArray(arrayStr);
- array = jsonArray.toArray();
- }
-
- return array;
- }
-
- /**
- * json字符串转成 JSONObject
- *
- * @param jsonStr json str
- * @return {@link JSONObject}
- */
- public JSONObject parseJson(String jsonStr) {
- JSONObject json = null;
-
- if (StrUtil.isNotBlank(jsonStr)) {
- json = JSONUtil.parseObj(jsonStr);
- }
-
- return json;
- }
- }
- package com.gnetek.ws.core.thymeleaf;
-
- import org.thymeleaf.context.IExpressionContext;
- import org.thymeleaf.expression.IExpressionObjectFactory;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
-
- /**
- * 表达式配置工厂
- *
- * @author mrchh
- * @date 2023/11/16
- */
- public class ThIExpressionObjectFactory implements IExpressionObjectFactory {
-
- private Map<String, Object> map;
-
- public ThIExpressionObjectFactory(Map<String, Object> map) {
- this.map = new HashMap<>(map);
- }
-
- /**
- * 获取所有表达式对象名称
- *
- * @return {@link Set}<{@link String}>
- */
- @Override
- public Set<String> getAllExpressionObjectNames() {
- return map.keySet();
- }
-
- /**
- * 根据表达式名字获取表达式对象
- *
- * @param iExpressionContext 表达语境
- * @param s 名称
- * @return {@link Object}
- */
- @Override
- public Object buildObject(IExpressionContext iExpressionContext, String s) {
- return map.get(s);
- }
-
- /**
- * 是否允许缓存
- *
- * @param s 名称
- * @return boolean
- */
- @Override
- public boolean isCacheable(String s) {
- return true;
- }
- }
- package com.gnetek.ws.core.thymeleaf;
-
- import org.springframework.stereotype.Component;
- import org.thymeleaf.dialect.AbstractDialect;
- import org.thymeleaf.dialect.IExpressionObjectDialect;
- import org.thymeleaf.expression.IExpressionObjectFactory;
-
- import java.util.HashMap;
- import java.util.Map;
-
- @Component
- public class ThIExpressionFactoryDialect extends AbstractDialect implements IExpressionObjectDialect {
-
- private final IExpressionObjectFactory iExpressionObjectFactory;
-
- protected ThIExpressionFactoryDialect() {
- super("DarrenExpression");
- //map中可以多个
- Map<String, Object> map = new HashMap<>();
-
- //配置我们刚才写的工具类,key为页面上引用的名字
- map.put("thJson", new ThJson());
-
- iExpressionObjectFactory = new ThIExpressionObjectFactory(map);
- }
-
- @Override
- public IExpressionObjectFactory getExpressionObjectFactory() {
- return iExpressionObjectFactory;
- }
- }
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)}]]