Java 隐藏字段值空白 Thymeleaf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25808433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:12:33  来源:igfitidea点击:

Hidden Field Value Blank Thymeleaf

javahtmlspring-mvcthymeleaf

提问by Kaushik Balasubramanain

I have a thymeleaf form which has 2 hidden fields. I specify the value of the hidden fields using th:value, and I bind these fields to an object.

我有一个百里香叶形式,它有 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>

I want to bind the quizID and sequenceNo fields with the respective fields in the object. Line 6 and 7 correctly resolve the value of sequence number/quiz id and display it. However, the same value is not resolved in the th:value tag inside the form. The value is empty and nothing gets bound to the object fields.

我想将 quizID 和 sequenceNo 字段与对象中的相应字段绑定。第 6 行和第 7 行正确解析序列号/测验 id 的值并显示它。但是,在表单内的 th:value 标记中没有解析相同的值。该值为空,并且没有任何内容绑定到对象字段。

View Source

查看源代码

Request your help here.

在此处请求您的帮助。

EDIT:

编辑:

The code works when I remove the th:field attribute from the hidden element. But I want to bind it to an object variable, so that the server can process it. `

当我从隐藏元素中删除 th:field 属性时,该代码有效。但是我想将它绑定到一个对象变量,以便服务器可以处理它。`

采纳答案by ziuu

For me helped setting th:field(or actually name) using th:attr

对我来说帮助设置th:field(或实际上name)使用th:attr

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

In my example I wanted to have value from ${question}but the target in input is questionIDs[i]

在我的例子中,我想从中获得价值,${question}但输入中的目标是questionIDs[i]

In simple problem like your name=answerIdshould be enough.

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

回答by Govardhana Rao Ganji

We can resolve your scenario in 2 ways

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

1st way:

第一种方式:

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

2nd way:

方式二:

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

If you used thymeleaf th:field="*{sequenceNo}", internal code of thymeleaf works like,

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

  • It will check is there any nameattribute to particular element, if it is available th:field value overrides on name attribute.

    name="sequenceNo"

  • It will check is there any idattribute to particular element, if it is not available with the name of th:field value new attribute added on i.e)id.

    id="sequenceNo"

  • 它将检查特定元素是否有任何name属性,如果它可用th:field 值覆盖 name 属性

    名称=“序列号”

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

    id="序列号"

回答by Victor MySon

it seems Thymeleaf does not recognize the hiddenfields. in order to fix it, try this:

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

  1. define the input as text (not as hidden).
  2. define an the style tag like "display:none"in order to hide the elements from the screen.
  1. 将输入定义为文本(而不是隐藏)。
  2. 定义一个样式标签"display:none",以便从屏幕上隐藏元素。

The result should be something like this:

结果应该是这样的:

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

Hope it helps.

希望能帮助到你。