Announcement Announcement Module
Collapse
No announcement yet.
Model is lost when forwarding from one controller from another Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 04:56 AM.

  • #2
    The Model will always be empty. You have simply attributes in your model so they should be available as request parameters.

    Comment


    • #3
      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?

      Comment


      • #4
        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...

        Comment


        • #5
          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.
          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.

          Comment

          Working...
          X