thymeleaf 直接調用後臺Service之${@myBean.doSomething()}

方法說明:

thymeleaf的官方文檔中提到了${@myBean.doSomething()}可以訪問容器中bean的數據。

前端thymeleaf寫法:


	<label>性別: </label>
		<select name="status" th:with="type=${@dict.getType('sys_sex')}">
			<option value="">--請選擇--</option>
			<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
		</select>
<-- js定義變量 -->
var datas = [[${@dict.getType('sys_sex')}]];	

拼接後臺獲取的數據 '__${ }__', 前後都是兩個下劃線 ,加單引號往後臺傳的時候是字符串,不加單引號往後臺傳的是數字, __${ }__

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

<div class="m-b" th:with="type=${@dict.getLabel('sys_sex','__${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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章