我正在用春季靴子(我是初学者)编写货币兑换器代码,现在我被卡在百里香模板上。现在,我的模板如下所示:
<div align="center">
<form th:action = "@{/postCurrency}" method = "POST">
<label for="firstNumber"></label>
<input id = "firstNumber" type="number" name = "fNumber"/>
<br/>
<br/>
<input type="submit" value="Submit">
</form>
</div>
因此,这意味着我有一个“框”,用户在其中键入数字,现在我想有第二个“框”,其中将交换货币并自动在该“框”中显示货币,我该怎么做? / p>
编辑:我的模板现在看起来像这样(exchange.html):
<form th:action = "@{/postCurrency}" method = "POST">
<label for="firstNumber"></label>
<input id = "firstNumber" type="number" name = "fNumber"/>
<br/>
<br/>
<input type="submit" value="Submit">
<br/>
<br/>
<input type = "number" th:field="*{resultNumber}" disabled/>
</form>
我的控制器类:
@PostMapping("/postCurrency")
public String postExchange(@RequestParam Double fNumber , Model model){
Double number = exchangeLogic.exchange(fNumber);
model.addAttribute("resultNumber",number);
return "redirect:/exchange";
}
问题是胸腺无法读取modelattribute,我需要输入“ resultNumber”并使其在表单标记中可见
答案 0 :(得分:0)
在您的控制器中尝试此操作。
@PostMapping("/postCurrency")
public String postExchange(@RequestParam Double fNumber , Model model){
Double number = exchangeLogic.exchange(fNumber);
model.addAttribute("resultNumber",number);
return "exchange";//your html page
}
以及您的html页面
<input type = "number" th:value="${resultNumber}" disabled/>
让我知道它是否对您有用。