当前,当我单击“退款”按钮时,$ {messages}和$ {changes}会显示在url上,而不是显示在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:/";
}

最佳答案

实际上,您的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:/";
}

09-25 22:19