2013-11-09 229 views
0

我有一个控制器,处理URL studentEdit.html。当我传递给使用AJAX的控制器值'get'时,我需要重定向到另一个页面viewStudents.html,但没有任何反应。我留在同一页面上。该方法返回ModelAndView,但它为什么不重定向到另一个我不知道的页面。Spring MVC。重定向页面

我也试着设置网址为:

重定向:viewStudents.html和重定向:viewStudents

,但它并没有帮助。

@RequestMapping(value = "/studentEdit.html", method = RequestMethod.GET) 
public ModelAndView getStudentProfile(@RequestParam(value = "get", required = false) String get) { 

    if(get != null && get.equals("get")) { 
     return new ModelAndView("redirect:/viewStudents.html"); 
    } else { 
     ModelAndView mav = new ModelAndView("studentEdit"); 
     return mav; 
    } 
} 


function getStudent() { 
      $.ajax({ 
       type: "GET", 
       url: "/IRSystem/studentProfileEdit.html", 
       data: "get=" + 'get', 
       success: function(response) { 

       }, 
       error: function(e) { 
        alert('Error' + e); 
       } 

      }); 
     } 

我将不胜感激任何信息,谢谢。

+0

是否有你在服务器端这样做的原因?如果你实际上没有从服务器上检索任何数据,你应该只使用Javascript重定向。 – ntaylor2

+0

您正在重定向AJAX请求本身。用这种方法重新定位页面是不可能的。 –

+0

你可以通过添加一个头来解决这个问题,例如'X-Redirect-Url:/ my/redirect'到响应中,并在成功回调中对该值执行操作。如果你的环境当然让你。 – Bart

回答

0

如果您想在请求后重定向到另一个页面,则不需要使用AJAX。

一个简单的链接应该是足够了:

<a href="/IRSystem/studentProfileEdit.html?get=get">click me</a> 

,当你点击链接会发生什么:

  • GET请求/IRSystem/studentProfileEdit.html进行
  • get=get作为请求数据
  • 传递
  • 默认情况下,用户被重定向到链接的href(即/IRSystem/studentProfileEdit.html),但与redirect:/viewStudents.html上述在一起,期望应该重定向可以重定向他别的

地方假设你的代码的其余部分是正确的,使用<a>

check the docsredirect:前缀,以确保您的重定向解释正确。

+0

谢谢。它有帮助。 – Vlad