0

I've got a problem with URL redirection. The URL should be changed by my Spring-MVC Controller - at least I guess so. This ma Let's consider such controller:

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value="/showLoginForm", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        return "loginView";
    }

    @RequestMapping(value="/verifyLogin", method = RequestMethod.POST)
    public String verifyLogin(@RequestParam String login, @RequestParam String password, ModelMap model) {
        //some stuff here
        return "redirect:/user/showLoginResult";
    }

    @RequestMapping(value="/showLoginResult", method = RequestMethod.GET)
    public String showLoginResultView(){
        return "loginResult";
    }
}

Initially user enters the URL : ../user/showLoginForm and fills the form, by clicking submit button request with data is being sent by ajax:

$.ajax({
    url: './verifyLogin',
    method: 'POST',
    data:{
          "login": login,
          "password": password
    },

    error: function(){
        //some stuff to do
    },
    success: function(data){
        //some stuff to do
        $("body").html( data ) //fetching data from request to browser, is this correct?
    }
});

Sent request triggers verifyLogin method to be invoked which should redirect URL to: user/showLoginResult, but certainly it does not. But then showLoginResultView method is invoked and it returns loginResult view to ajax callback.

To be clear:
1. I'm using Spring 4.0.4.RELEASE
2. I have already registered org.springframework.web.servlet.view.UrlBasedViewResolver instance.

Now the question: Why URL doesn't change?

Thanks in advance

bpawlowski
  • 1,025
  • 2
  • 11
  • 22

2 Answers2

2

but certainly it does not. But then showLoginResultView method is invoked

That means that it does do the redirection, and that the browser follows the redirection, and that the final response to the original AJAX request is given to the success callback of your AJAX handler. There's no reason for the URL in the address bar to change, since you're not sending a regular HTTP request, but an AJAX request.

See also: Returning redirect as response to XHR request

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I think these requests are the same HTTP requests. But the request I send is not send directly from the browser(through the URL), but through AJAX, that's why browser does not follow URL change, right? Going further response goes to AJAX success callback(not to the browser) where I should handle content change or URL change manually, right? – bpawlowski May 03 '14 at 17:20
  • I wouldn't say it that way, but you basically got it right. – JB Nizet May 03 '14 at 17:23
0

You can use RedirectView, it supports relative redirection (to some extent)

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/view/RedirectView.html

Daniil
  • 88
  • 1
  • 8