【问题标题】:Hidden Field Value Blank Thymeleaf隐藏字段值空白 Thymeleaf
【发布时间】: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 的类具有sequenceNoquizID 属性吗?
  • 是的,我确定。为了测试它,我在第 6 行和第 7 行中使用了它。它也显示在“查看源”图像中,分别为

    6

    1

  • 可以分享您的项目示例吗?
  • 你为什么同时使用th:valueth:field?如果值属于表单中使用的对象,您应该只使用th:field

标签: java html spring-mvc thymeleaf


【解决方案1】:

我们可以通过两种方式解决您的情况

第一种方式:

<input type="hidden" th:value="${question.id}" th:attr="name='quizID'" />

第二种方式:

<input type="hidden" th:value="${question.id}" name="quizID" />

如果你使用 thymeleaf th:field="*{sequenceNo}",thymeleaf 的内部代码就像,

  • 它将检查特定元素是否有任何 name 属性,如果它可用 th:field value overrides on name attribute

    name="sequenceNo"

  • 它将检查特定元素是否有任何 id 属性,如果在 ie)id 上添加的 th:field value 新属性的名称不可用。

    id="sequenceNo"

【讨论】:

    【解决方案2】:

    对我来说,使用th:attr 设置th:field(或实际上是name

    th:value="${question.id}"
    th:attr="name='questionIds[' + ${iter.index} + ']'"
    

    在我的示例中,我希望获得来自 ${question} 的值,但输入中的目标是 questionIDs[i]

    像你的name=answerId 这样的简单问题就足够了。

    【讨论】:

    • 你为我节省了很多时间和头痛。非常感谢:)
    【解决方案3】:

    Thymeleaf 似乎无法识别 hidden 字段。为了修复它,试试这个:

    1. 将输入定义为文本(而不是隐藏)。
    2. 定义一个样式标签,如"display:none",以便从屏幕上隐藏元素。

    结果应该是这样的:

    <input type="text" th:field="*{parameters[${stat.index}].field}" style="display:none;">
    

    希望对你有帮助。

    【讨论】:

      【解决方案4】:

      假设您必须收集对页面的评论。然后,除了注释之外,您还必须将页面名称传输给控制器。当然,用户不必重新输入该页面的名称。此信息必须传递给控制器​​,但 th:field 仅映射用户输入的值,而不是默认生成的值。 但是您可以将此页面的名称作为 URL 中的参数传输给控制器。 在 html 中,你有类似的东西:

      <form  th:action="@{/saveComment(lastPage=${lastPage})}" th:object="${comments}" method="post" enctype="multipart/form-data">
              <div class="row">
       .................................................................................
                  <h2>Enter your comment</h2>
                  <textarea  th:field="${comments.comment}" rows="10" cols="100" name="comment" id="comment"></textarea>
                  <label for="comment">Your comment here</label><br />
                  <input type="submit" name ="submit" value="Submit" />
              </div>
          </form> 
      
      In controller, you put stuff like this:
      
         @PostMapping("/saveComment")
          public String saveComment(Comments comments, String lastPage) {
              comments.setPage_commented(lastPage);
              commentsRepository.save(comments);
              return "redirect:/";
          }
      

      【讨论】:

        猜你喜欢
        • 2013-09-03
        • 1970-01-01
        • 1970-01-01
        • 2019-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-26
        相关资源
        最近更新 更多