Issue
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
Solution
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
Answered By - JB Nizet
Answer Checked By - Clifford M. (JavaFixing Volunteer)