简体   繁体   English

Thymeleaf-将变量传递给控制器

[英]Thymeleaf - passing a variable to controller

I have a problem with Spring and Thymeleaf. 我对Spring和Thymeleaf有问题。

I have a controller: 我有一个控制器:

@RequestMapping(value = "/add")
public String addPage(@PathVariable("id") String id, Model model) {
    InvoiceData invoiceData = new InvoiceData();
    model.addAttribute("contractorid", id);
    model.addAttribute("invoicedata", invoiceData);
    return "add";
}

And I have an URI: 我有一个URI:

<li class="menu"><a th:href="@{/add/{id}(id=${contractor.id})}">Add invoice</a>

and of course, I have this add.html document containing a form to fill and save data to ContractorData . 当然,我有这个add.html文档,其中包含一个表单,用于填充数据并将其保存到ContractorData

But when I click the link, I get: 但是,当我单击链接时,我得到:

There was an unexpected error (type=Not Found, status=404).
No message available

And the URL in the browser looks like this: 浏览器中的URL如下所示:

http://localhost:8080/add/5c9e31b05b9b380a6b08dc94

So it is completely basing on the basic URI 因此它完全基于基本URI

How do I modify the URI or my code so I can pass the contractor.id to the /add controller and put it into Model so it will be available for me to use in add.html? 如何修改URI或代码,以便可以将contractor.id传递到/ add控制器并将其放入Model以便可以在add.html中使用它?

It should help, i think. 我认为应该会有所帮助。

@RequestMapping(value = "/add/{id}")
public String addPage(@PathVariable("id") String id, Model model) {
    InvoiceData invoiceData = new InvoiceData();
    model.addAttribute("contractorid", id);
    model.addAttribute("invoicedata", invoiceData);
    return "add";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM