Thymeleaf带条件渲染模型列表报错,请求技术支持
解决Thymeleaf模板处理异常:无法解析表达式
你的问题出在Thymeleaf表达式的语法错误上,我来帮你拆解问题并修正:
错误原因分析
- 条件判断逻辑错误:你写的
(${storeInfo.storeStatus} == 'InActive'||'Active')不符合Thymeleaf的布尔表达式规则,||两侧必须都是完整的布尔判断,而你右边直接写'Active'会被当成字符串,无法解析为布尔值。而且从需求来看,你应该是想让InActive状态的选项显示红色,正常状态显示黑色,所以只需要判断是否等于InActive即可。 - 样式表达式格式错误:Thymeleaf的三元表达式返回值需要是合法的样式字符串,你写的
(color:red):(color:black)格式不对,应该把样式写成带引号的字符串形式。
修正后的代码
<select id="storeDropDown" th:field="*{storeId}" name="storeId" class="form-control input-md mb-md"> <option value="NA" th:text="#{reports.selectStore}"></option> <option th:each="storeInfo : ${storeInfoList}" th:value="${storeInfo.storeId}" th:style="${storeInfo.storeStatus == 'InActive'} ? 'color: red' : 'color: black'" th:text="${storeInfo.storeName}" th:name="${storeInfo.storeStatus}"> </option> </select>
额外说明
- 我去掉了原代码中第一个
<option>标签里多余的th:name="stareStatus"(拼写还错了,应该是storeStatus),这个属性在下拉选项里没必要,还会和循环里的th:name冲突。 - 三元表达式里的判断可以简化为
${storeInfo.storeStatus == 'InActive'},不需要额外加括号包裹整个判断,Thymeleaf能正确解析。
内容的提问来源于stack exchange,提问作者Totanagouda patil
相关产品推荐
相关产品推荐

