java如何将数据从thymeleaf HTML页面发送到MVC spring引导控制器?
我不熟悉thymeleaf和spring,我试图将简单的输入数据从索引页发送到控制器,因此我创建了一个对象交换,如下所示,其中将包含要处理的输入字段。问题是,我不断收到图片中显示的错误
这是我的交换对象
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..";
}
# 1 楼答案
使用@modeldattribute从索引页获取数据到控制器-
# 2 楼答案
我认为您应该在控制器中使用@ModelAttribute来捕获Exchange对象,如下所示: