java - Thymeleaf th :field handle null with input text but not with radio, 为什么?

标签 java html spring thymeleaf

我是 Thymeleaf 的新手,在此之前文档对我帮助很大。但几个小时以来我一直停留在表格上并进行了大量研究!我正在以 HTML 形式预加载数据测试(稍后将来自数据库)。

Java Spring Controller

@RequestMapping(value = "/foobar", method = RequestMethod.GET)
public String foobar(@RequestParam(value="bar", required = true) String id, Model model) {

    Obj foo = new Obj();
    foo.setPropertyOne(1.71f);
    foo.setPropertyTwo(null);

    model.addAttribute("foo", foo);

    return "foobar";
}

属性不断增加。我没有在 POJO Obj 上放置任何 @Annotation。 (?)

我很高兴发现 th:field 对输入文本的作用就像一个魅力。

<form th:object="${foo}" action="#" method="post">
    <input type="text" th:field="*{propertyOne}" />
</form>

如果存在文本数据,则预加载,如果没有(设置为 null 甚至根本不设置),则输入保持为空。

让我们转向 radio 类型。

<input value="false" type="radio" th:field="*{propertyX}"/>
<input value="true" type="radio" th:field="*{propertyX}"/>

它仍然有效,根据 boolean 值检查 false 或 true。但如果它没有设置或设置为 null,我就会得到 org.thymeleaf.spring4.processor.attr.SpringInputRadioFieldAttrProcessor ;显然 Thymeleaf 不处理 RadioField 上的 null?

我尝试使用 th:checked 代替,但也不处理 null:

<input value="false" type="radio" th:checked="!*{propertyX}"/>
<input value="true" type="radio" th:checked="*{propertyX}"/>

我还尝试了 th:if 的不同表达式,但没有成功。

我希望我给了你足够的信息!感谢您的帮助!

最佳答案

谢谢@Metroid,我正要编辑我的第一条消息以添加整个异常。这是一篇很大的文章,通常通过阅读它我会找到解决方案......我可能没有读得足够透彻。 :x

在发帖之前我又读了一遍,突然我意识到,在第三行:org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)

正如我告诉过你的,我首先使用了 boolean 值,然后使用了 boolean 值,但忘记更改 setter/getter !它仍然是 isPropertyX 而不是 getPropertyX,我想 Thymeleaf 无法访问该方法!

现在正在运行。不管怎样,谢谢,多亏我们的交流,我才来到这里!

关于java - Thymeleaf th :field handle null with input text but not with radio, 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47077558/

相关文章:

java - Selenium WebDriver 和 Java 机器人类

java - Spring AOP : Aspect not working on called method

html - 力元素向上移动

jquery - 仅在其上设置带有滚动条的内容 div

html - 如何在 CSS 中为表格做这件事

java - 依赖注入(inject)如何在没有构造函数的情况下实例化一个类?

java - 找不到javax.annotation :com. springsource.javax.annotation :jar:1. 0.0 失败

java - 通信 c 服务器和 java 客户端时出错

java - 如何在不覆盖序列化方法的情况下为特定类覆盖 JsonSerializer 的 isEmpty 方法?

java - 对与 HashMap 连接的模型的 ArrayList 进行排序