如何在胸腺中初始化外部参考?

发布于 2025-02-06 18:33:49 字数 1322 浏览 1 评论 0原文

如果我有一个名为“自助餐”的Java类,其中一个字段就是这样:

@ManyToOne
private Chef chef

我必须使用胸腺以HTML形式创建一个新的自助餐对象,那么我如何才能参考此厨师领域?目前,我的HTML看起来像这样:

<form th:action="@{/newBuffet}" method="POST" th:object="${buffet}">
    <div>
        <div>
            <span><input type="text" th:field="*{name}" placeholder="name"/></span>
        </div>      
        <div>
            <span><input type="text" th:field="*{chef}" placeholder="chef"/></span>
        </div>
        <div>
            <span><button type="submit">Invio<i class="material-icons">done</i></button></span>
        </div>
    </div>
</form>

我的控制器像这样:

@RequestMapping(value = "/newBuffet", method = RequestMethod.GET)
public String formBuffet (Model model) {
    model.addAttribute("buffet", new Buffet());
    return "newBuffet.html";
}

@RequestMapping(value = "/newBuffet", method = RequestMethod.POST)
public String addBuffet (@ModelAttribute("buffet") Buffet b, Model model) {
    this.buffetService.save(b);
    model.addAttribute("buffets", this.buffetService.listBuffets());
    return "buffets.html";
}

当然,由于胸腺的输入的方式,我的输入的方式当然是因为我的输入是我的班级厨师的一个实例。

If I had a Java class called for instance Buffet and one of its fields was something like:

@ManyToOne
private Chef chef

and I had to create a new Buffet object in an html form using Thymeleaf, how would I be able to refer to this chef field? At the moment my html looks like this:

<form th:action="@{/newBuffet}" method="POST" th:object="${buffet}">
    <div>
        <div>
            <span><input type="text" th:field="*{name}" placeholder="name"/></span>
        </div>      
        <div>
            <span><input type="text" th:field="*{chef}" placeholder="chef"/></span>
        </div>
        <div>
            <span><button type="submit">Invio<i class="material-icons">done</i></button></span>
        </div>
    </div>
</form>

and my controller like this:

@RequestMapping(value = "/newBuffet", method = RequestMethod.GET)
public String formBuffet (Model model) {
    model.addAttribute("buffet", new Buffet());
    return "newBuffet.html";
}

@RequestMapping(value = "/newBuffet", method = RequestMethod.POST)
public String addBuffet (@ModelAttribute("buffet") Buffet b, Model model) {
    this.buffetService.save(b);
    model.addAttribute("buffets", this.buffetService.listBuffets());
    return "buffets.html";
}

the error it gives is, of course, because of the way Thymeleaf's input takes only strings whilst my input is an instance of my class Chef.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文