【发布时间】:2014-11-06 15:23:36
【问题描述】:
我有一个 thymeleaf 表单,它有 2 个隐藏字段。我使用 th:value 指定隐藏字段的值,并将这些字段绑定到一个对象。
<div class="w-row">
<div class="w-col w-col-6">
<div class="question_text_sc">
<p th:text="${questionVO.questionText}" />
<p th:text="${questionVO.questionStem}" />
<p th:text="${sequenceNo}" />
<p th:text="${quizID}" />
</div>
</div>
<div class="question_stem_sc"></div>
<div class="w-col w-col-6">
<div>
<div class="w-form">
<form class="w-clearfix" id="email-form" name="email-form" data-name="Email Form" action="#" th:action="@{/quiz/question}" th:object="${userResponseVO}" method="post">
<div th:each="option: ${questionVO.answerOptions}" class="w-radio radio_select" th:id="${'radio_1'}">
<input class="w-radio-input" id="radio" type="radio" name="answer_sc" th:field="*{answerID}" th:value="${option}"/>
<label class="w-form-label" id="answer_1" for="radio"><p th:text="${option}" /></label>
</div>
<input type="hidden" name="sequenceNo" th:field="*{sequenceNo}" th:value="${sequenceNo}" ></input>
<input type="hidden" name="quizID" th:field="*{quizID}" th:value="${quizID}"></input>
<button class="button submit_answr" type="submit">Next Question</button>
</form>
我想将 quizID 和 sequenceNo 字段与对象中的相应字段绑定。第 6 行和第 7 行正确解析了序列号/测验 id 的值并显示它。但是,相同的值不会在表单内的 th:value 标记中解析。该值为空,并且没有任何内容绑定到对象字段。
在这里请求您的帮助。
编辑:
当我从隐藏元素中删除 th:field 属性时,该代码有效。但我想将它绑定到一个对象变量,以便服务器可以处理它。 `
【问题讨论】:
-
您确定支持
userResponseVO的类具有sequenceNo和quizID属性吗? -
是的,我确定。为了测试它,我在第 6 行和第 7 行中使用了它。它也显示在“查看源”图像中,分别为
6
和1
。 -
可以分享您的项目示例吗?
-
你为什么同时使用
th:value和th:field?如果值属于表单中使用的对象,您应该只使用th:field
标签: java html spring-mvc thymeleaf