开发者

spring mvc: don't redirect after login - need two models?

开发者 https://www.devze.com 2023-03-11 01:11 出处:网络
I coded a wee login controller. It has an onSubmit method which logs in the user. If the login is successful I want to show the front page without having to redirect. The front page needs content from

I coded a wee login controller. It has an onSubmit method which logs in the user. If the login is successful I want to show the front page without having to redirect. The front page needs content from some other model. Beca开发者_开发知识库use my LoginController already has a LoginModel it can't also have the InformationModel.

Is there some way to get a pointer on the InformationModel? Or some call to get the ModelAndView of the InformationController? That controller provides a handleRequest method.

I think this is more a fundamental question, but if you need code to answer it I will supply it.


I',m not sure if I get your question correctly, but

a.) You can add multiple models on your ModelAndView object. Use:

 modelAndView.addObject("informationModel", informationModelObject);

b.) If successful login, set the view to your front page view:

 modelAndView.setView("frontPageView");

To access your InformationController on your LoginController, you can autowire it =)

@Autowired
InformationController informationController;

    @RequestMapping( ... ) // assuming you define it here
    public ModelAndView onSubmit(... ) {
      // .. code here

      if (loginsuccess) {
          InformationModel informationModelObject = informationController.handleRequest(...);
           modelAndView.addObject("informationModel", informationModelObject);
          modelAndView.setView("frontPageView");
      }
      else {
         modelAndView.setView("loginFailView");
      }

      return modelAndView;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消