目前,当我点击退款按钮时,$ {messages}和$ {changes}会显示在网址上,而不是显示在jsp内部。
网址看起来像这个localhost:8080 / name /?messages = Nice + Try%21& changes = Quarters%3A + 0 +%7C + Nickels%3A + 0%0ADimes%3A + 0 +%7C + Pennies %3A + 0
根本没有任何东西出现在jsp体内。
我试过c:out value =" $ {changes}",它没有用。 我也尝试将requestMethod更改为POST,没有运气。
块引用
JSP
<div class='row text-center' id='change-display'>
<div id="change">
${changes} <---- should display the string, but nothing shows up here.
</div>
</div>
<div class='row text-center' id='getChangeButtonDiv'>
<a href="Money/refund">
<button class='btn btn-purchase' type='button' id='refund-button'>
<p>Refund</p> <------- refund button
</button>
</a>
</div>
@RequestMapping(value="/refund", method=RequestMethod.GET)
public String refund(Model model) throws PersistenceException{
BigDecimal zero = new BigDecimal(0);
BigDecimal total = service.calculateInsertedTotal();
int quarterInt=0, dimeInt=0, nickelInt=0, pennyInt=0;
String message="Here is your refund!";
if(total.compareTo(zero) != 1){
message="Nice Try!";
}else{
while(total.compareTo(quarter)==0||total.compareTo(quarter)== 1){
quarterInt ++;
total = total.subtract(quarter);
}
while(total.compareTo(dime)==0||total.compareTo(dime)== 1){
dimeInt ++;
total = total.subtract(dime);
}
while(total.compareTo(nickel)==0||total.compareTo(nickel)== 1){
nickelInt ++;
total = total.subtract(nickel);
}
while(total.compareTo(nickel)==0||total.compareTo(nickel)== 1){
nickelInt ++;
total = total.subtract(nickel);
}
}
String quarterString = "Quarters: "+ quarterInt;
String dimeString = "Dimes: "+ dimeInt;
String nickelString = "Nickels: "+ nickelInt;
String pennyString = "Pennies: "+ pennyInt;
String changes = quarterString + " | " + nickelString
+"\n"+dimeString + " | " + pennyString;
service.removeMoney();
model.addAttribute("messages", message);
model.addAttribute("changes", changes);
return "redirect:/";
}
答案 0 :(得分:1)
实际上,由于您使用attributes
代替redirect:/
,forward
正在丢失,当您从一个请求重定向到另一个请求时,新的 http请求已创建,因此旧请求附加的model
将丢失,这意味着您的属性消息和更改也将丢失,因此要解决此问题,您必须将您的属性保存在RedirectAttributes
:
@RequestMapping(value="/refund", method=RequestMethod.GET)
public String refund(Model model, RedirectAttributes redirectModel) throws PersistenceException{
//...
redirectModel.addFlashAttribute("messages", message);
redirectModel.addFlashAttribute("changes", changes);
return "redirect:/";
}