簡體   English   中英

Spring MVC會話:預期的會話屬性“用戶”,已在會話中存在

[英]Spring MVC Session: Expected Session attribute 'user', which already there in Session

我面對

org.springframework.web.HttpSessionRequiredException: Expected session attribute "Money"

以下是一系列步驟:

1)用戶從控制器獲取頁面:Finance / MoneyCreation

@Controller
@RequestMapping("/Finance/*")
@SessionAttributes({ "Money", "MoneyForm"})
public class MoneyController {


  @RequestMapping(value = "MoneyCreation", method = RequestMethod.GET)
   public String openMoneyLandingPage(@ModelAttribute("Money") Money money,
   Model model) {
   model.addAttribute("MoneyForm", form);
   return "/okonomi/okonomiregister";
}

在上面的控制器中,會話屬性Money不在其他地方設置(這是必需的嗎?。現在,我必須從UI中打開單擊按鈕的對話框,該對話框執行以下操作:

AjaxController:財務/創建/金錢

@Controller
@RequestMapping("/Finance/*")
@SessionAttributes({ "Money", "MoneyForm" })
public class AjaxMoneyController{

@RequestMapping(value = "create/money", method = RequestMethod.POST)
 public String openDialogBox(@ModelAttribute("Money") User user, Model  model) {

return "/commonProcess/dialog/MoneyDialog";
}   

點擊按鈕; 我正面臨:

org.springframework.web.HttpSessionRequiredException: Expected session attribute "Money"

我是否需要將Money設置在其他位置?

是的,您需要在引用該屬性之前將其放入http會話中。

引用非常好的Spring文檔(您也應該研究它!),您將看到一個示例閱讀

if (!model.containsAttribute("site")) {
    model.addAttribute("site", new PetSite());
}

“ site”是SessionAttribute(在您的情況下為“ money”)。

您需要在使用對象之前將其加載到會話中。 標准方法是使用@ModelAttribute 方法

@ModelAttribute("Money")
Money getMoney() {
  return new Money(); //or however you create a default
}

在每次請求之前都會調用getMoney()

在用戶界面中,我添加了轉化ID,此問題已得到解決。

var convId = $('#formId').find('input[name="_CONV_ID"]').val();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM