Spring MVC url重定向
我的URL重定向有问题。URL应该由我的SpringMVC控制器更改——至少我想是这样。这位妈妈 让我们考虑这样的控制器:Spring MVC url重定向,spring,url,spring-mvc,redirect,url-redirection,Spring,Url,Spring Mvc,Redirect,Url Redirection,我的URL重定向有问题。URL应该由我的SpringMVC控制器更改——至少我想是这样。这位妈妈 让我们考虑这样的控制器: @Controller @RequestMapping("/user") public class UserController { @RequestMapping(value="/showLoginForm", method = RequestMethod.GET) public String printWelcome(ModelMap model) {
@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";
}
}
最初,用户输入URL:../user/showloginfo并通过单击submit按钮填写表单。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?
}
});
发送请求触发要调用的verifyLogin方法,该方法应将URL重定向到:user/showLoginResult,但肯定不会。但随后调用showLoginResultView方法,并将loginResult视图返回给ajax回调
需要说明的是:1.我正在使用Spring 4.0.4。发布
2.我已经注册了org.springframework.web.servlet.view.UrlBasedViewResolver实例。
现在的问题是:为什么URL不变?
提前谢谢 但肯定不是这样。但随后会调用showLoginResultView方法 这意味着它确实执行重定向,浏览器遵循重定向,并且对原始AJAX请求的最终响应将提供给AJAX处理程序的成功回调。没有理由更改地址栏中的URL,因为您发送的不是常规HTTP请求,而是AJAX请求
另请参见:您可以使用RedirectView,它支持相对重定向(在某种程度上)
我认为这些请求是相同的HTTP请求。但是我发送的请求不是直接从浏览器发送的(通过URL),而是通过AJAX发送的,这就是为什么浏览器不遵循URL更改,对吗?更进一步的响应是AJAX成功回调(而不是浏览器),我应该手动处理内容更改或URL更改,对吗?我不会这么说,但基本上你是对的。