0

I have a method GET in js

$(".btn-sm").click(function() {
        $.ajax({
            url: '/check_rating/'+this.value,
            type: 'GET',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            success: function (response) {
                alert(response.status);
            },
            error: function () {
                alert("error");
            }
        });
    });

url is such like this /check_rating/1. Controller

 @RequestMapping(value = "/check_rating/{id}",method = RequestMethod.GET)
    public String check_rating(@PathVariable("id")Long id, RedirectAttributes redirectAttributes){
        List<Rating>rating = ratingService.findAllRatingsByIdStudentAndStageOfApproveGreaterThan(id,0);
        redirectAttributes.addFlashAttribute("rating",rating);
        return "redirect:/students_rating";
    }

    @RequestMapping(value = "/students_rating",method = RequestMethod.GET)
    public String student_rating(@ModelAttribute("rating") List<Rating>rating, ModelMap model){
        model.addAttribute("rating",rating);
        return "students_rating";
    }
}

I need redirect to /students_rating, but after sending get method by url /check_rating/1 i still remain on the same page and redirect is now working, but on console i have log such this

MODEL = {rating=[student_rating.entity.Rating@7856e7f, student_rating.entity.Rating@6a369ebf, student_rating.entity.Rating@7ed68627], org.springframework.validation.BindingResult.rating=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'students_rating'; URL [/pages/students_rating.jsp]] in DispatcherServlet with name 'dispatcher'
DEBUG o.s.web.servlet.view.JstlView - Added model object 'rating' of type [java.util.ArrayList] to request in view with name 'students_rating'
o.s.web.servlet.view.JstlView - Added model object 'org.springframework.validation.BindingResult.rating' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'students_rating'
o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'requestDataValueProcessor'
o.s.web.servlet.view.JstlView - Forwarding to resource [/pages/students_rating.jsp] in InternalResourceView 'students_rating'
o.s.web.servlet.DispatcherServlet - Successfully completed request
o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

students_rating.jsp

<tbody id="tBody">
            <c:forEach items="${requestScope.rating}" var="rating">
            <tr><td class="column"><c:out value="${rating.id}"></c:out></td><td><c:out value="${rating.date}"></c:out></td><td><c:out value="${rating.score}"></c:out></td></tr>
            </c:forEach>
            </tbody>
  • 1
    Possible duplicate of [redirect in Spring MVC](https://stackoverflow.com/questions/4584410/redirect-in-spring-mvc) – Rui Lopes Jun 05 '18 at 12:58
  • this dont solve my problem – Taras Danylchenko Jun 05 '18 at 13:09
  • 1
    See [redirected-page-is-not-being-displayed-with-spring-mvc-ajax-request](https://stackoverflow.com/questions/48545348/redirected-page-is-not-being-displayed-with-spring-mvc-ajax-request/48546984#48546984) – Tap Jun 05 '18 at 13:31

2 Answers2

0

You can not redirect page with Location header for ajax requests. You must get some response text for ajax and redirect user using parsed response text. This is how to redirect browser with JS code

if (responseText == 'OK') { 
    document.location.href = 'http://example.com/';
}
0

If you are hitting controller using AJAX request then Your Spring mvc controller should return some response and based on the response you can redirect the request in your view page.

Update ajax call to below code :

$(".btn-sm").click(function() {
        $.ajax({
            url: '/check_rating/'+this.value,
            type: 'GET',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            success: function (response) {
                if (responseText == 'OK') { 
                  window.location.href = '<--replace this with controller URL-->';
                }
            },
            error: function () {
                alert("error");
            }
        });
    });

You could use document.location.href but it is deprecated in html5.

Alien
  • 15,141
  • 6
  • 37
  • 57