效果图展现地址, 轻易点击一个帖子查看上方链接就行
这里 thymeleaf 的版本号是 3.x
package com.daishu.bbs.config;
import com.daishu.bbs.util.AESUtil;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.dialect.AbstractDialect;
import org.thymeleaf.dialect.IExpressionObjectDialect;
import org.thymeleaf.expression.IExpressionObjectFactory;
import java.util.Collections;
import java.util.Set;
/**
* @author : xuzhennan @Project : daishu @Package : com.daishu.bbs.config
* @email : xuzhennan@58.com
* @date : 2022 年 03 月 02 日 下午 12:01
*/
public class CustomDialect extends AbstractDialect implements IExpressionObjectDialect {public CustomDialect() {super("Custom Dialect");
}
@Override
public IExpressionObjectFactory getExpressionObjectFactory() {return new IExpressionObjectFactory() {
@Override
public Set<String> getAllExpressionObjectNames() {return Collections.singleton("aesUtil");
}
@Override
public Object buildObject(IExpressionContext context, String expressionObjectName) {return new AESUtil();
}
@Override
public boolean isCacheable(String expressionObjectName) {return true;}
};
}
}
@Configuration
public class ThymeleafConfig {
@Bean
public CustomDialect myDialect() {return new CustomDialect();
}
}
<a th:href="@{|/article/detail/${#aesUtil.encrypt(post.id)}|}" th:utext="${post.title}"></a>
如果 tyhmeleaf 版本号是 2.x
public class MyDialect extends AbstractDialect implements IExpressionEnhancingDialect {public MyDialect() {super();
}
@Override
public String getPrefix() {
// @see org.thymeleaf.dialect.IDialect#getPrefix
return "xxx";
}
@Override
public boolean isLenient() {return false;}
@Override
public Map<String, Object> getAdditionalExpressionObjects(IProcessingContext ctx) {Map<String, Object> expressions = new HashMap<>();
expressions.put("aesUtil", new AESUtil());
return expressions;
}
}