I am trying to make a html page called balance where user inputs an account number. What's the best way to retrieve the input value to the controller? I tried this and I don't know if this is the right way.
我正在尝试制作一个名为balance的html页面,用户输入一个帐号。检索控制器输入值的最佳方法是什么?我试过这个,我不知道这是不是正确的方法。
@PostMapping("/balance")
public String balanceCheck(@RequestParam("aNum") Long num,@ModelAttribute Transactions user,Model model) {
int deposits=0;
int withdraw=0;
int balance=0;
long accountNum= user.getAccountNum();
Iterable <Transactions>accountTrans= transactRepository.findByAccountNum(user.getAccountNum());
for (Transactions tran: accountTrans){
if (tran.getAction().equals("D")){
deposits+=tran.getAmmount();
}
else if (tran.getAction().equals("W")){
withdraw+=tran.getAmmount();
}
}
balance=deposits-withdraw;
model.addAttribute("balance", balance);
return "balance" ;
}
And this is my html:
这是我的HTML:
<form action="#" th:action="@{/balance}" method="post">
<p> ACCOUNT #: <input type="number" name="aNum "/></p>
<input type="submit" value="Submit"/>
</form>
1 个解决方案
#1
0
To print the value on the page, you'll want:
要在页面上打印值,您需要:
<form action="#" th:object="${user}" th:action="@{/balance}" method="post">
<p>ACCOUNT #: <input type="number" th:name="aNum"/></p>
<input type="submit" value="Submit"/>
</form>
Then create a @GetMapping
that will include model.addAttribute("user", user);
however you plan to get/create the user.
然后创建一个包含model.addAttribute(“user”,user)的@GetMapping;但是你计划获得/创建用户。
By doing this, you're telling Spring to bind the values to the user
bean.
通过这样做,您告诉Spring将值绑定到用户bean。
You can remove the space after aNum
and after the <P>
tags too. It sounds really perfectionist, but code formatted improperly is error-prone and harder for the next person to read.
您可以删除aNum之后和
标签之后的空格。这听起来真的很完美,但是格式不正确的代码容易出错,下一个人阅读起来也比较困难。
#1
0
To print the value on the page, you'll want:
要在页面上打印值,您需要:
<form action="#" th:object="${user}" th:action="@{/balance}" method="post">
<p>ACCOUNT #: <input type="number" th:name="aNum"/></p>
<input type="submit" value="Submit"/>
</form>
Then create a @GetMapping
that will include model.addAttribute("user", user);
however you plan to get/create the user.
然后创建一个包含model.addAttribute(“user”,user)的@GetMapping;但是你计划获得/创建用户。
By doing this, you're telling Spring to bind the values to the user
bean.
通过这样做,您告诉Spring将值绑定到用户bean。
You can remove the space after aNum
and after the <P>
tags too. It sounds really perfectionist, but code formatted improperly is error-prone and harder for the next person to read.
您可以删除aNum之后和
标签之后的空格。这听起来真的很完美,但是格式不正确的代码容易出错,下一个人阅读起来也比较困难。