![](/img/trans.png)
[英]How to send an object from a thymeleaf form to MVC spring boot controller
[英]How to send data from thymeleaf HTML page to the MVC spring boot controller?
我是 thymeleaf 和 spring 的新手,我試圖將簡單的輸入數據從索引頁發送到控制器,所以我創建了一個如下所示的對象 Exchange,其中將包含要處理的輸入字段。 問題是我不斷收到圖片中顯示的錯誤
這是我的 Exchange 對象
package myprivate.work.MyCurrencyConverter.model;
public class Exchange
{
private String fromcurrency;
private String tocurrency;
private double amount;
public Exchange(){
super();
}
public Exchange(String fromcurrency, String tocurrency, double amount) {
super();
this.fromcurrency = fromcurrency;
this.tocurrency = tocurrency;
this.amount = amount;
}
public String getFromcurrency() {
return fromcurrency;
}
public void setFromcurrency(String fromcurrency) {
this.fromcurrency = fromcurrency;
}
public String getTocurrency() {
return tocurrency;
}
public void setTocurrency(String tocurrency) {
this.tocurrency = tocurrency;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
這是我在索引中的表格
<form th:action="@{/newexchange}" th:object="${exchange}" method='POST'>
<p>Select From Currency:</p>
<p>
<select th:field="*{fromcurrency}"> <!-- this is line 12 -->
<option th:value="sek">SEK</option>
<option th:value="eur">EUR</option>
<option th:value="usd">USD</option>
<option th:value="jpy">JPY</option>
</select>
</p>
<p>Select To Currency:</p>
<p>
<select th:field="*{tocurrency}">
<option th:value="sek">SEK</option>
<option th:value="eur">EUR</option>
<option th:value="usd">USD</option>
<option th:value="jpy">JPY</option>
</select>
</p>
<p class="form"><label>Insert New Rate:</label><input type="number" th:field="*{amount}"/>
</p>
<p><input name="Convert" type="submit" value="submit"/></p>
</form>
在我的控制器中
@RequestMapping(method = RequestMethod.POST , value = "/newexchange")
public String toExchange(Exchange exchange, BindingResult result)
{
return "..ok..";
}
你必須告訴 Thymeleaf 你指的是哪個對象。 您必須將ModelAttribute
傳遞給頁面,以便 Thymeleaf 知道將屬性綁定到哪個對象。
使用星號語法*{...}
計算th:object
上所選對象的表達式。
像這樣的東西:
// this method need to show your exchange page
@GetMapping("/exchange")
public String handleGetRegisterRequest(
@ModelAttribute Exchange exchange) { // your object (Thymeleaf will receive this to bind with the parameters you pass)
return "exchange"; // the name of your page which contains the exchange informations
}
我認為您應該在控制器中使用 @ModelAttribute 來捕獲這樣的 Exchange 對象:
Controller:
@RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
Foo foo = new Foo();
foo.setBar("bar");
model.addAttribute("foo", foo);
...
}
@RequestMapping(value = "/processForm", method=RequestMethod.POST)
public String processForm(@ModelAttribute(value="foo") Foo foo) {
...
}
html :
<form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
<input type="text" th:field="*{bar}" />
<input type="submit" />
</form>
Model Data :
public class Foo {
private String bar;
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
使用@ModelAttribute 將數據從索引頁獲取到控制器-
@RequestMapping(method = RequestMethod.POST , value = "/newexchange")
public String toExchange(@ModelAttribute Exchange exchange, BindingResult result)
{
logger.logInfo("Exchange Amount->>"+exchange.getAmount());
return "..ok..";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.