Announcement Announcement Module
Collapse
No announcement yet.
Redirect to new controller method does not change url Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 03:58 PM.

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

    Comment


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

      Comment


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

        Comment


        • #5
          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, 06:39 AM.

          Comment


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

            Comment


            • #7
              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, 06:38 AM.

              Comment


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

                Comment


                • #9
                  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!!

                  Comment

                  Working...
                  X