th:attr
时间: 2023-07-16 13:14:46 浏览: 52
`th:attr` 是 Thymeleaf 模板引擎提供的一个属性,用于设置 HTML 元素的属性值。它的语法如下:
```
th:attr="<attributeName>:<attributeValue>"
```
这个语法的作用是将 `attributeName` 属性的值设置为 `attributeValue`,例如:
```html
<a th:attr="href:@{/home}">Home</a>
```
这个例子中,Thymeleaf 会将 `<a>` 标签的 `href` 属性设置为 `/home`,其中 `@{/home}` 是 Thymeleaf 的 URL 表达式,用于生成相对于应用程序根路径的 URL。
相关问题
<button class="btn btn-lg btn-success btn-block" th:attr="uri=@{/stu/login}" onclick="send(0)" id="bt0">什么意思
```html
<button class="btn btn-lg btn-success btn-block" th:attr="uri=@{/stu/login}" onclick="send(0)" id="bt0">什么意思?
```
这是一个HTML代码示例,其中包含一个按钮元素,具有类名为"btn btn-lg btn-success btn-block",并且具有一个id为"bt0"。该按钮还包含一个th:attr属性,用于设置URI,并且有一个onclick属性,当点击按钮时会调用send(0)函数。
html 复选框添加时为空,修改复选框<div class="form-group"> <label class="col-sm-3 control-label">专长:</label> <div class="col-sm-8" th:with="type=${@dict.getType('xqd_zc_lx')}"> <label th:each="dict : ${type}" class="check-box"> <input name="zhuanchang" type="checkbox" th:value="${dict.dictValue}" th:text="${dict.dictLabel}" th:attr="checked=${jcyJcyxx.zhuanchang.contains(dict.dictValue)?true:false}"> </label> </div> </div>为空 修改时报错 Exception evaluating SpringEL expression: "jcyJcyxx.zhuanchang.contains(dict.dictValue)?true:false" (template: "jcy/jcyxx/edit" - line 258, col 123)
根据你提供的代码,修改时报错的原因可能是因为 `jcyJcyxx` 对象中的 `zhuanchang` 属性为空,无法执行 `contains` 方法。你可以在修改前先判断 `zhuanchang` 是否为空,如果为空则给它一个默认值,比如一个空的列表。可以尝试修改代码如下:
```
<div class="form-group">
<label class="col-sm-3 control-label">专长:</label>
<div class="col-sm-8" th:with="type=${@dict.getType('xqd_zc_lx')}">
<label th:each="dict : ${type}" class="check-box">
<input name="zhuanchang" type="checkbox" th:value="${dict.dictValue}" th:text="${dict.dictLabel}" th:attr="checked=${jcyJcyxx.zhuanchang != null and jcyJcyxx.zhuanchang.contains(dict.dictValue) ? true : false}">
</label>
</div>
</div>
```
这里使用了一个简单的逻辑判断 `jcyJcyxx.zhuanchang != null and jcyJcyxx.zhuanchang.contains(dict.dictValue)`,如果 `zhuanchang` 不为空且包含 `dict.dictValue`,则勾选复选框。如果 `zhuanchang` 为空,则不勾选复选框。希望这能帮到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)