Results 1 to 9 of 9

Thread: Redirect to new controller method does not change url

  1. #1
    Join Date
    Apr 2012
    Posts
    13

    Question Redirect to new controller method does not change url

    hi there, this is my first post...

    i'm new to spring and i'm stuck in a little problem...

    I've a controller with 3 methods like these:
    Code:
    @RequestMapping(value = {"","/list"}, method = RequestMethod.GET)
    	public MyModelAndView getCustomerList() {
    		MyModelAndView mav = new MyModelAndView();
    		mav.setViewName("default");
    		mav.addObject("component", "customers");
    		mav.addObject("pageTitle", "Customers Management");
    		mav.addObject("page", "index");
    		
    		List<Customer> customersList = customerBo.list();
    		mav.addObject("customersList",customersList);
    
    		return mav;
    	}
    
    @RequestMapping(value = "/new", method = RequestMethod.GET)
    	public MyModelAndView newCustomer() {
    		MyModelAndView mav = new MyModelAndView();
    		mav.setViewName("default");
    		mav.addObject("component", "customers");
    		mav.addObject("pageTitle", "New Customer");
    		mav.addObject("page", "customer");
    
    		mav.addObject("customer", new Customer());
    
    		return mav;
    	}
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    	public MyModelAndView saveCustomer(@Valid Customer customer, BindingResult result) {
    		if (result.hasErrors()) {
    			Message errorMessage = new Message();
    			errorMessage.setType(MessageType.MESSAGE_ERR);
    
    			for (ObjectError err : result.getAllErrors()) {
    				errorMessage.addText(err.getDefaultMessage());
    			}
    
    			MyModelAndView mav = newCustomer();
    
    			mav.addObject(MessageType.MESSAGE_ERR.getType(), errorMessage);
    			mav.addObject("customer", customer);
    
    			return mav;
    		} else {
    			customerBo.save(customer);
    			return getCustomerList();
    		}
    
    	}
    When i go to "/" or "/list" getCustomerList() is called and i got the list of the customers;
    in this page i've a button that point to /new and that brings me to the new customer form.
    Now the problem....when i save the form I validate the bean, in case of success I persist the object and then I return the getCustomerList() method, but, while the view changes, the url in the browser remains "/save"...

    I'd like to display "/" or "/list"...
    how can i do it?

    thanks
    Last edited by fsteccanella; Apr 4th, 2012 at 02:58 PM.

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

    Default

    First post in the correct forum, second use [ code][/code ] tags when posting code that way people can actually read the code.

    Nice that yiou return the result from the getCustomerList but that doesn't change the url. If you want to change it you have to issue a redirect to the /list URL.

    Also why are you reinventing the wheel with your own copying of validation messages? Spring has out-of-the-box support for this. The same for hardcoding those page titles spring has I18N support and MessageSources to help you with those, you don't want that stuff in yuour controller.
    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 2012
    Posts
    13

    Default

    sorry for the wrong section...if someone can move the thread in the right one i'll appreciate...
    however i've also tried with the
    Code:
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    	public ModelAndView saveCustomer(@Valid Customer customer, BindingResult result) {
    		if (result.hasErrors()) {
    			Message errorMessage = new Message();
    			errorMessage.setType(MessageType.MESSAGE_ERR);
    
    			for (ObjectError err : result.getAllErrors()) {
    				errorMessage.addText(err.getDefaultMessage());
    			}
    
    			MyModelAndView mav = newCustomer();
    
    			mav.addObject(MessageType.MESSAGE_ERR.getType(), errorMessage);
    			mav.addObject("customer", customer);
    
    			return mav;
    		} else {
    			customerBo.save(customer);
    			return new ModelAndView("redirect:/list");
    		}
    
    	}
    with no success...

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

    Default

    It should be the complete URL so if /list isn't the complete URL it isn't going to. Also as I mentioned before I find your code not very clean...
    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 2012
    Posts
    13

    Default

    The complete URL is http://localhost:8080/TimeSheet/customers/ or http://localhost:8080/TimeSheet/customers/list...
    I've tried with redirect:/list....I think that is correct, because the getCustomerList() is called and all is working fine, except the browser url...

    ps: how do you mean with "not very clean"? how do you would you write it?

    Thanks
    Last edited by fsteccanella; Apr 5th, 2012 at 05:39 AM.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,628

    Default

    The redirect should be redirect:/customers/list that should change the browser URL as it will issue a client side redirect. Your initial approach doesn't.

    I wouldn't reuse logic (calling a controller method from another controller method) and I would leverage spring to do the heavy lifting (model attributes, validation/errors messages) you aren't working with but against or around spring. I already gave you some pointers in the previous posts.
    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

  7. #7
    Join Date
    Apr 2012
    Posts
    13

    Default

    thank u Marten, using redirect:/customers/list works!!!
    However I've tried to not reuse logic:

    Code:
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    	public MyModelAndView saveCustomer(@Valid Customer customer, BindingResult result) {
    		if (result.hasErrors()) {
    			Message errorMessage = new Message();
    			errorMessage.setType(MessageType.MESSAGE_ERR);
    
    			for (ObjectError err : result.getAllErrors()) {
    				errorMessage.addText(err.getDefaultMessage());
    			}
    
    			MyModelAndView mav = new MyModelAndView("redirect:/customers/new");
    
    			mav.addObject(MessageType.MESSAGE_ERR.getType(), errorMessage);
    			mav.addObject("customer", customer);
    
    			return mav;
    		} else {
    			customerBo.save(customer);
    			return new MyModelAndView("redirect:/customers/list");
    		}
    
    	}
    but underline models are not stored in request...
    I'd simply like to stay on the same page of the request ("/customers/new") and add these two models to display errors... (we use these messages because we use the same core for information, warning and error messages..)
    Last edited by fsteccanella; Apr 5th, 2012 at 05:38 AM.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,628

    Default

    Don't redirect. A redirect destroys your model. If you really want to redirect (and you are on Spring 3.1) use RedirectAttributes to store those objects as flash attributes.

    And as I stated instead of a ModelAndView simply return a String with the name of the view to render.

    I also wonder why you don't use springs error messages/spring messages support to show your erorr messages but are including them yourselves.
    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

  9. #9
    Join Date
    Apr 2012
    Posts
    13

    Default

    Thank you so much Marten...with the combination of RedirectAttributes and @ModelAttribute I have solved all my issue!
    Now I'll look into springs error messages...

    I apologize for the noob questions that I did!!

Tags for this Thread

Posting Permissions

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