如何将胸腺themeaf解析为SpringbootMVC? [胸腺弹药启动java17]
因此,我在HTML中有一个硬编码的true/false值,因此在HTML表单提交后,我可以使用它来填充我的一个实体的E.Bool值(post)。
HTML
// I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option value="1">true option</option>
<option value="0">false option</option>
</select>
// then I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option th:value="1">true option</option>
<option th:value="0">false option</option>
</select>
// then I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option th:value="${true}">true option</option>
<option th:value="${false}">false option</option>
</select>
实体
@Entity
@Table
public class E {
..
@Column
boolean bool
..
}
控制器
public String addE(@Valid E e, BindingResult result, HttpServletRequest request, Model model) {
...
// no matter what, E has bool = false here :(
...
}
我在做什么错?无论我尝试什么,它总是错误的。
最疯狂的是,我可以从字面上去我的Intellij并在调试器中看到,请求。参数在那里具有正确的值!这是怎么回事?为什么胸腺/春季如此困难?是否有任何更简单的方法可以通过控制器获得这样的布尔值?
So I have a hardcoded true/false value in HTML so I can use that to fill the E.bool value for one of my entities after the HTML form submits (POST).
HTML
// I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option value="1">true option</option>
<option value="0">false option</option>
</select>
// then I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option th:value="1">true option</option>
<option th:value="0">false option</option>
</select>
// then I tried
<select id="isBool" name="isBool" class="form-select" aria-label="select">
<option th:value="${true}">true option</option>
<option th:value="${false}">false option</option>
</select>
Entity
@Entity
@Table
public class E {
..
@Column
boolean bool
..
}
Controller
public String addE(@Valid E e, BindingResult result, HttpServletRequest request, Model model) {
...
// no matter what, E has bool = false here :(
...
}
What am I doing wrong? No matter what I try, it's always false.
The craziest thing is that I can literally go to my IntelliJ and see in the debugger, that the request.parameters has the correct value right there! What's going on? Why is ThymeLeaf/Spring being so difficult? Is there any easier way to get a boolean like this through the controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令人惊讶的是,这是布尔成员菲尔德(Boolean Member Field)的出现故障。我的IDE将其作为“ isbool”而不是“ getBool”产生,显然胸腺仅寻找字面的getters和setters。
Amazingly, it was the getters fault for the boolean member field. My IDE generated it as "isBool" instead of "getBool" and apparently Thymeleaf only looks for literal getters and setters.