【问题标题】:Name tag is changing when using th:field使用 th:field 时名称标签正在更改
【发布时间】:2021-03-07 05:17:07
【问题描述】:

我想用 thymeleaf 制作一个简单的表单,它只使用一些数据,其余的正常显示给用户。但我被困在 thymeleaf th:field 更改 HTML 标记 - 名称上。这样做的结果是表单无法正常工作 - 我可以检查一些选项。 我希望只能选择一个选项。这是我写的代码:

<div class="row justify-content-center">
        <div class="text-center" th:object="${post}">
            <h2 th:text="*{title}">Title</h2>
            <div>
                <label th:text="*{shortDescription}">Short description</label>
            </div>
            <form action="#" method="post" th:action="@{/post/{id}(id=${post.id})}" class="mt-5">
                <div class="form-group">
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="option1" name="option" class="custom-control-input" value="1" th:field="*{option1}">
                        <label class="custom-control-label" th:text="*{option1.getOptionValue() + ' - ' + option1.getVoteAmount()}"></label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="option2" name="option" class="custom-control-input" value="2" th:field="*{option2}">
                        <label class="custom-control-label" th:text="*{option2.getOptionValue() + ' - ' + option2.getVoteAmount()}"></label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="option3" name="option" class="custom-control-input" value="3" th:field="*{option3}">
                        <label class="custom-control-label" th:text="*{option3.getOptionValue() + ' - ' + option3.getVoteAmount()}"></label>
                    </div>
                </div>
                <button class="btn btn-primary" type="submit">Vote</button>
            </form>
        </div>
    </div>

这是我的控制器:

@PostMapping("/post/{id}")
    public String updateRoom(@ModelAttribute("id") Integer id, String option1, String option2, String option3 , Model model) {
        Post post = postModel;
        postService.increaseVoteCounter(option1, option2, option3, post);
        model.addAttribute("post", postModel);
        return "detailsPost";
    }

PostModel 类是这样的:

public class PostModel {
    private String username;
    private String option1;
    private String option2;
    private String option3;
    private String shortDescription;
    private String title;

    //getters and setters
}

最后它在浏览器中的样子: image

【问题讨论】:

  • 也许您可以尝试标记错误所在的行以及您的期望,以使您的问题更清楚
  • 已更改 - 我希望只能选择一个选项,但 thymeleaf (th:field) 正在更改无线电形式的名称标签。

标签: java spring spring-boot thymeleaf


【解决方案1】:

th:field 总是更改 nameid

你不应该在这里使用它

read more about this

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2021-06-21
    • 1970-01-01
    • 2011-03-27
    • 2019-02-19
    相关资源
    最近更新 更多