Thymeleaf中textarea中的th:field不起作用。

huangapple go评论69阅读模式
英文:

Thymeleaf th:field in textarea not working

问题

我在几种语言中有一段描述,使用Thymeleaf时,我发现textarea中的th:field不起作用!

与此同时,input中的th:text或th:value起作用,并在POST请求时更新数据。

请帮助我找出原因

<label>描述</label>
<textarea id="description" th:field="*{description}" placeholder="输入内容">
</textarea>

// 这个有效,但我需要使用<textarea>
<input type="text" th:value="${description}" th:name="description"/>

// 控制器
@GetMapping("/{language}/post/{id}/edit")
public String editPost(@PathVariable Language language,
                               @PathVariable Long id,
                               Model model) throws Exception {

    model.addAttribute("post", postService.getPost(id));
    model.addAttribute("language", language.getCode());

    String description = postService.getDescription(id, language);
    model.addAttribute("description", description);
    return "admin/post-edit";
}

尝试了不同的选项 - 没有帮助(((

英文:

I have a description in several languages, using thymeleaf I came across the fact that th:field in textarea not working!

At the same time th:text or th:value in input works and updates the data on POST request.

Please help me find the cause

enter image description here

&lt;label&gt;Description&lt;/label&gt;
&lt;textarea id=&quot;description&quot; th:field=&quot;*{description}&quot; placeholder=&quot;Enter content&quot;&gt;
&lt;/textarea&gt;

//This works, but I need to use &quot;textarea&quot;
&lt;input type=&quot;text&quot; th:value=&quot;${description}&quot; th:name=&quot;description&quot;/&gt;

//Controller
@GetMapping(&quot;/{language}/post/{id}/edit&quot;)
    public String editPost(@PathVariable Language language,
                                   @PathVariable Long id,
                                   Model model) throws Exception {

        model.addAttribute(&quot;post&quot;, postService.getPost(id));
        model.addAttribute(&quot;language&quot;, language.getCode());

        String description = postService.getDescription(id, language);
        model.addAttribute(&quot;description&quot;, description);
        return &quot;admin/post-edit&quot;;
    }

Tried different options - nothing helps (((

答案1

得分: 1

你可以使用 `th:text` 来设置 `<textarea>` 的内容:


<details>
<summary>英文:</summary>

You can use `th:text` to set the content of the `&lt;textarea&gt;`:

<textarea id="description" th:name="description" th:text="${description}"
placeholder="Enter content"></textarea>


</details>



huangapple
  • 本文由 发表于 2023年3月23日 09:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定