thymeleaf 直接调用后台Service

前端thymeleaf

<select name="sex" class="form-control m-b" th:with="type=${@dict.getType('sys_user_sex')}">
         <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>

拼接后台获取的数据 '__${ }__', 前后都是两个下划线 ,加单引号往后台传的时候是字符串,不加单引号往后台传的是数字, __${ }__

<input  class="form-control m-b" th:with="type=${@dict.getLabel('sys_user_sex','__${hrmResource.sex}__')}" th:value="${type}" >
             

<div class="m-b" th:with="type=${@dict.getLabel('sys_blood','__${hrmResource.bloodType}__')}">[[${type}]]</div>

后台 Spring boot


/**
 * html调用 thymeleaf 实现字典读取
 */
@Service("dict")
public class DictService
{
    @Autowired
    private IDictDataService dictDataService;

    /**
     * 根据字典类型查询字典数据信息
     * 
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<DictData> getType(String dictType)
    {
        return dictDataService.selectDictDataByType(dictType);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue)
    {
        return dictDataService.selectDictLabel(dictType, dictValue);
    }
}

原文地址:https://www.cnblogs.com/lick468/p/10966132.html