我在js中有一个方法GET
$(".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就像这样/ check_rating / 1。调节器
@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";
}
}
我需要重定向到/ students_rating,但是在通过url / check_rating / 1发送get方法之后我仍然保持在同一页面上并且重定向现在正在工作,但是在控制台上我已经记录了这样的
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>
您无法使用Location标头重定向页面以获取ajax请求。您必须为ajax获取一些响应文本,并使用已解析的响应文本重定向用户。这是如何使用JS代码重定向浏览器
if (responseText == 'OK') {
document.location.href = 'http://example.com/';
}
如果您使用AJAX请求命中控制器,那么您的Spring mvc控制器应该返回一些响应,并根据响应您可以在视图页面中重定向请求。
更新以下代码的ajax调用:
$(".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");
}
});
});
您可以使用document.location.href,但在html5中不推荐使用它。