You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSP转Thymeleaf报错:属性名称不能为空

解决Thymeleaf中"Attribute name cannot be null or empty"错误

兄弟,你这个问题的根源其实特别明显——你的Thymeleaf表单字段的语法写错了!

看你页面里的input标签:

<input type="text" th:field:="*{userName}" placeholder="Username (*)" class="form-control" />

这里的th:field:多了一个额外的冒号!Thymeleaf的属性语法是th:属性名="表达式",正确的写法应该是th:field="*{userName}",这个多余的冒号会让Thymeleaf解析器无法识别合法的属性名称,直接抛出了"Attribute name cannot be null or empty"的异常。

另外你密码输入框的placeholder还写错成了"First Name (*)",这个是小问题,顺手改了更好;同时密码输入框的type改成password会更合理。修正后的表单字段代码应该是这样:

<!-- User name -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" th:field="*{userName}" placeholder="Username (*)" class="form-control" />
</div>
<!-- Password -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" th:field="*{password}" placeholder="Password (*)" class="form-control" />
</div>

这个错误和你的DTO验证、Controller逻辑完全无关——你之前移除验证没解决问题也印证了这一点。只要把所有th:field:的多余冒号去掉,Thymeleaf就能正常解析表单绑定了,页面应该就能正常加载了。

内容的提问来源于stack exchange,提问作者OrangeFlavour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:34:58