简体   繁体   中英

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

I am facing

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

Following are series of steps:

1) User gets a page from controller: 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";
}

In above controller, the session attribute Money is not set somewhere else (is that required?. Now from UI i have have to open a dialogue on button click which executes following:

AjaxController: finance/create/money

@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";
}   

On click of button; i am facing:

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

Do i need to set Money somewhere else?

Yes, you need to put the attribute into the http session before referring to it.

Quoting the very good Spring docs (you ought to look into, too!), you see an example reading

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

"site" being a SessionAttribute (like "money" in your case).

You need to load the object into session before using it. The standard way is to use a @ModelAttribute method :

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

getMoney() will get called before every request.

在用户界面中,我添加了转化ID,此问题已得到解决。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM