Spring MVC。重定向页面

时间:2013-11-09 19:01:15

标签: java ajax spring-mvc

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

我还尝试将网址设置为:

重定向:viewStudents.html和redirect: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);
                }

            });
        }

感谢您的任何信息,谢谢。

1 个答案:

答案 0 :(得分:0)

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

一个简单的链接就足够了:

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

点击链接时会发生什么:

  • 执行了对/IRSystem/studentProfileEdit.html的GET请求
  • get=get作为请求数据传递
  • 默认情况下,用户被重定向到链接的href(即/IRSystem/studentProfileEdit.html),但您可以将其重定向到其他地方

假设您的其余代码是正确的,那么使用上面提到的<a>redirect:/viewStudents.html应该根据需要重定向。

另外check the docs关于redirect:前缀,以确保正确解释您的重定向。