thymeleaf自定义表达式
2020-01-03 本文已影响0人
爱的旋转体
1.自定义表达式需要的工具类(个人业务相关)
package com.lhtc.jv.utils;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lhtc.jv.config.Constants;
import com.lhtc.jv.domain.DictType;
import com.lhtc.jv.domain.DictValue;
import com.lhtc.jv.service.DictTypeService;
/**
* 字典工具类
* @author xuzhipeng
* @date 2019-09-02 14:17:02
* @since 1.0
*/
@Component
public class DictUtils {
private static final String DEFAULT_VALUE = "";
private static DictTypeService dictTypeService;
private static RedisUtil redisUtil;
public static void clearCache(){
redisUtil.del(Constants.REDIS_KEY_DICT);
redisUtil.del(Constants.REDIS_KEY_DICT_MAP);
redisUtil.delByPattern(Constants.REDIS_KEY_DICT_TYPE_PREFIX+Constants.SYMBOL_ASTERISK);
}
@SuppressWarnings("unchecked")
public static List<DictType> getDictList(){
List<DictType> dictList = (List<DictType>)redisUtil.get(Constants.REDIS_KEY_DICT);
if(CollectionUtils.isEmpty(dictList)){
dictList = dictTypeService.findList();
Map<String, List<DictValue>> dictMap = Maps.newHashMap();
for(DictType dictType : dictList){
dictMap.put(dictType.getType(), Lists.newArrayList(dictType.getDictValues()));
}
redisUtil.set(Constants.REDIS_KEY_DICT, dictList);
redisUtil.set(Constants.REDIS_KEY_DICT_MAP, dictMap);
}
return dictList;
}
@SuppressWarnings("unchecked")
public static Map<String, List<DictValue>> getDictMap(){
Map<String, List<DictValue>> dictMap = (Map<String, List<DictValue>>)redisUtil.get(Constants.REDIS_KEY_DICT_MAP);
if(CollectionUtils.isEmpty(dictMap)){
List<DictType> dictList = dictTypeService.findList();
dictMap = Maps.newHashMap();
for(DictType dictType : dictList){
dictMap.put(dictType.getType(), Lists.newArrayList(dictType.getDictValues()));
}
redisUtil.set(Constants.REDIS_KEY_DICT, dictList);
redisUtil.set(Constants.REDIS_KEY_DICT_MAP, dictMap);
}
return dictMap;
}
public static List<DictValue> getDictValueList(String type){
List<DictValue> dictValueList = Lists.newArrayList();
if(StringUtils.isNotBlank(type)){
Map<String, List<DictValue>> dictMap = getDictMap();
if(!CollectionUtils.isEmpty(dictMap)){
dictValueList = dictMap.get(type);
}
}
if (dictValueList == null){
dictValueList = Lists.newArrayList();
}
return dictValueList;
}
@SuppressWarnings("unchecked")
public static Map<String, DictValue> getDictValueMap(String type){
Map<String, DictValue> dictValueMap = Maps.newHashMap();
if(StringUtils.isNotBlank(type)){
String key = RedisKeyUtil.getKey(Constants.REDIS_KEY_DICT_TYPE_PREFIX, type);
dictValueMap = (Map<String, DictValue>)redisUtil.get(key);
if(CollectionUtils.isEmpty(dictValueMap)){
dictValueMap = Maps.newHashMap();
Map<String, List<DictValue>> dictMap = getDictMap();
if(!CollectionUtils.isEmpty(dictMap)){
List<DictValue> dictValueList = dictMap.get(type);
if(!CollectionUtils.isEmpty(dictValueList)){
for(DictValue dictValue : dictValueList){
if(StringUtils.isNotBlank(dictValue.getName())){
dictValueMap.put(dictValue.getName(), dictValue);
}
}
redisUtil.set(key, dictValueMap);
}
}
}
}
if (dictValueMap == null){
dictValueMap = Maps.newHashMap();
}
return dictValueMap;
}
public static List<String> getDictValueStringList(String type){
List<String> dictValueStringList = Lists.newArrayList();
if(StringUtils.isNotBlank(type)){
Map<String, List<DictValue>> dictMap = getDictMap();
if(!CollectionUtils.isEmpty(dictMap)){
List<DictValue> list = dictMap.get(type);
for(DictValue dictValue : list){
if(StringUtils.isNotBlank(dictValue.getName())){
dictValueStringList.add(dictValue.getName());
}
}
}
}
if (dictValueStringList == null){
dictValueStringList = Lists.newArrayList();
}
return dictValueStringList;
}
public static String getDictValueValue(String type, String name, String defaultValue){
if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(name)){
for (DictValue dictValue : getDictValueList(type)){
if (name.equals(dictValue.getName())){
return dictValue.getValue();
}
}
}
return defaultValue;
}
public static String getDictValueValue(String type, String name){
return getDictValueValue(type, name, DEFAULT_VALUE);
}
public static String getDictValueName(String type, String value, String defaultName){
if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)){
for (DictValue dictValue : getDictValueList(type)){
if (value.equals(dictValue.getValue())){
return dictValue.getName();
}
}
}
return defaultName;
}
public static String getDictValueName(String type, String value){
return getDictValueName(type, value, DEFAULT_VALUE);
}
@Autowired
public void setRedisUtil(RedisUtil redisUtil) {
DictUtils.redisUtil = redisUtil;
}
@Autowired
public void setDictTypeService(DictTypeService dictTypeService) {
DictUtils.dictTypeService = dictTypeService;
}
}
2.定义一个表达式对象的工厂类
package com.lhtc.jv.config;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.expression.IExpressionObjectFactory;
import com.lhtc.jv.utils.DictUtils;
/**
* thymeleaf自定义表达式
* @author xuzhipeng
* @date 2020-01-02 16:28:50
* @since 1.0
*/
@Component
public class BaseDialectExpressionFactory implements IExpressionObjectFactory {
public static final String EXPRESSION_NAME_DICT_UTILS = "dict";
public static final Set<String> ALL_EXPRESSION_OBJECT_NAMES;
@Autowired
private DictUtils dictUtils;
static {
final Set<String> allExpressionObjectNames = new LinkedHashSet<String>();
allExpressionObjectNames.add(EXPRESSION_NAME_DICT_UTILS);
ALL_EXPRESSION_OBJECT_NAMES = Collections.unmodifiableSet(allExpressionObjectNames);
}
@Override
public Set<String> getAllExpressionObjectNames() {
return ALL_EXPRESSION_OBJECT_NAMES;
}
@Override
public Object buildObject(IExpressionContext context, String expressionObjectName) {
if(null == expressionObjectName)
return null;
Object obj = null;
switch (expressionObjectName) {
case EXPRESSION_NAME_DICT_UTILS:
obj = dictUtils;
break;
default:
break;
}
return obj;
}
@Override
public boolean isCacheable(String expressionObjectName) {
if(EXPRESSION_NAME_DICT_UTILS.equals(expressionObjectName)) {
return true;
}else {
return false;
}
}
}
3.自定义Dialect
package com.lhtc.jv.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;
/**
* @author xuzhipeng
* @date 2020-01-02 16:46:22
* @since 1.0
*/
@Component
public class BaseDialect implements IExpressionObjectDialect{
@Autowired
private BaseDialectExpressionFactory baseDialectExpressionFactory;
@Override
public IExpressionObjectFactory getExpressionObjectFactory() {
return baseDialectExpressionFactory;
}
@Override
public String getName() {
return "Base";
}
}
4.thymeleaf页面
<select>
<option value="">请选择</option>
<option
th:each="item : ${#dict.getDictValueList('school_age')}"
th:value="${item.value}" th:text="${item.name}">
</option>
</select>
参考:1.https://blog.csdn.net/edzhou00/article/details/83310707
2.https://blog.csdn.net/whatlookingfor/article/details/78423031