Spring MVC。 重定向页面

I have a controller wich handles URL studentEdit.html. When I pass to controller value 'get' using AJAX I need to redirect to another page viewStudents.html, but nothing happens. I stay on the same page. The method returns the ModelAndView, but why it does not redirect to another page I do not know.

I also tried to set url as:

redirect:viewStudents.html and redirect:viewStudents

but it did not helped.

@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);
                }

            });
        }

I would appreciate any information, thank you.

If you want to redirect to another page after request, there is no need to use AJAX.

A simple link should be enough:

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

What happens when you click on the link:

  • a GET request to /IRSystem/studentProfileEdit.html is performed
  • get=get is passed as request data
  • by default, user is redirected to link's href (i.e /IRSystem/studentProfileEdit.html), but you can redirect him somewhere else

Assuming the rest of your code is correct, using <a> as mentioned above together with redirect:/viewStudents.html should redirect as desired.

Also check the docs about redirect: prefix to make sure your redirect is interpreted correctly.