Results 1 to 5 of 5

Thread: Model is lost when forwarding from one controller from another

  1. #1
    Join Date
    Apr 2010
    Location
    Kharkov, Ukraine
    Posts
    52

    Default Model is lost when forwarding from one controller from another

    I have 2 controllers:
    Code:
    @Controller
    public class StartController {
    	@RequestMapping(value = "/start")
    	public ModelAndView start(){
    		HashMap<String, Object> model = new HashMap<String, Object>();
    		model.put("hello", "world");
    		return new ModelAndView("forward:/end", model);
    	}
    }
    
    @Controller
    public class EndController {
    	@RequestMapping(value = "/end")
    	public ModelAndView end(Model model){
                   System.out.println(model); //model is empty
                   return null;
    	}
    }
    Anytime I hit StartController, it forwards to EndController, but the Model in EndController is always empty.
    I suspect I'm doing something fundamentally wrong, since the issue persists in both 3.1.0 and 3.2.1, but I couldn't google out the answer why this happens. Can anyone suggest?
    Last edited by denis_k; Feb 28th, 2013 at 04:56 AM.
    Regards,
    Denis

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    The Model will always be empty. You have simply attributes in your model so they should be available as request parameters.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2010
    Location
    Kharkov, Ukraine
    Posts
    52

    Default

    Quote Originally Posted by Marten Deinum View Post
    The Model will always be empty. You have simply attributes in your model so they should be available as request parameters.
    Thanks for the answer.
    You probably meant that I can pull 'hello' parameter from the request like this
    Code:
    @Controller
    public class EndController {
    	@RequestMapping(value = "/end")
    	public ModelAndView end(Model model, HttpServletRequest request){
                   System.out.println(model); //model is empty
                   System.out.println(request.getAttribute('hello')); //this outputs 'world'
                   return null;
    	}
    }
    This works. Is there no purpose in declaring Model parameter in a controller at all?
    Regards,
    Denis

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    This works. Is there no purpose in declaring Model parameter in a controller at all?
    Not in the way you want to use it. The model only has purpose for the current method the content of the model isn't passed on the the next method. The forward could be to a servlet or whatever so spring does make no assumptions regarding this.

    You probably meant that I can pull 'hello' parameter from the request like this
    You should use getParameter not getAttribute...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2010
    Location
    Kharkov, Ukraine
    Posts
    52

    Default

    Quote Originally Posted by Marten Deinum View Post
    Not in the way you want to use it. The model only has purpose for the current method the content of the model isn't passed on the the next method. The forward could be to a servlet or whatever so spring does make no assumptions regarding this.
    Thanks, this makes sense now.
    Quote Originally Posted by Marten Deinum View Post
    You should use getParameter not getAttribute...
    Strange, in my case, request.getParameter returns null, but request.getAttribute returns what is expected.
    Regards,
    Denis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •