我正在返回 Controller 下面的模型 View 属性表单
@RequestMapping(value = "/signin", method = RequestMethod.POST)
public @ResponseBody
ModelAndView loginUser(@RequestParam(value = "userName") String userName,
@RequestParam(value = "password") String password,
ModelAndView model) {
on success login
return new ModelAndView("redirect:/another controller here");
on failure login
return new ModelAndView("same page");
我有调用此 Controller 的 jquery ajax 调用,当成功登录时我想让用户进入另一个页面,但我的响应到达同一页面而不是重定向到另一个页面。
需要帮助
编辑 - 我正在使用基于 URL 的 View 解析器和 Apache tiles 进行 View 渲染
最佳答案
假设我理解您的问题,重定向将在 AJAX 调用的上下文中进行 - 因此 JQuery AJAX 代码将遵循重定向并接收从 /another controller here
返回的响应.这不会触发整个页面刷新,但 JQuery AJAX 代码将提供响应以在当前页面中呈现。
要在成功登录时更改整个页面,您最好不要为此屏幕使用 AJAX。
或者,您可以返回一些指示符来告诉 JQuery 重定向。例如:
@RequestMapping(value = "/signin", method = RequestMethod.POST)
public @ResponseBody
ModelAndView loginUser(@RequestParam(value = "userName") String userName,
@RequestParam(value = "password") String password,
ModelAndView model) {
on success login
ModelAndView modelAndView = new ModelAndView("same page");
modelAndView.addObject("redirectUrl", "/another controller here");
return modelAndView;
on failure login
return new ModelAndView("same page");
然后在 same page
中加入一些 Javascript寻找 redirectUrl
的存在并触发整页重定向(如果存在)。
var redirect = '${redirectUrl}';
if (redirect) {
window.location.replace(redirect);
}
关于javascript - spring modelandview 重定向在 jquery ajax 调用中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19375896/