今天遇到了一个问题,需要把收藏功能优化,在用户未登录的情况下点击收藏时跳转到登录界面。
踩了一些坑后发现由于AJAX请求异步刷新的原因,不支持转发和重定向。
解决办法
1、直接由前台判断是否转发
$.ajax({
url:"/room/insertHotelCollectionByHotelId",
contentType:"application/json;charset=UTF-8",
data:'{"hotelId":'+hotelId+'}',
dataType:"json",
type:"post",
success:function(data) {
},
error:function (data) {
window.location.href="/userhtml/login.html";
}
});
@ResponseBody
@RequestMapping("insertHotelCollectionByHotelId")
public ResultData insertHotelCollectionByHotelId(HttpServletRequest request, @RequestBody String body) throws IOException, ServletException {
ResultData resultData = new ResultData();
Long userId = Long.parseLong(request.getSession().getAttribute("userId").toString());
if (userId == null) {
resultData.setCode(3);
resultData.setMessage("数据为空");
} else {
Gson gson = new Gson();
HotelCollection hotelCollection = gson.fromJson(body,HotelCollection.class);
hotelCollection.setUserId(userId);
roomService.insertHotelCollectionByHotelId(hotelCollection);
resultData.setCode(0);
resultData.setData(hotelCollection);
}
return resultData;
}
2、后台过滤器判断这个请求是否为AJAX请求,如果是,则向前台发送包含这个信息的cookie,前台再根据cookie判断,决定是否转发。
public class MyInterceptor extends HandlerInterceptorAdapter{
//该方法是进入controller的必经之路。
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if(是否登录了?){
return true;
}else { //没有登录重定向到登录页面
reDirect(request, response);
return false;
}
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
super.afterCompletion(request, response, handler, ex);
}
//对于请求是ajax请求重定向问题的处理方法
public void reDirect(HttpServletRequest request, HttpServletResponse response) throws IOException{
//获取当前请求的路径
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+request.getContextPath();
//如果request.getHeader("X-Requested-With") 返回的是"XMLHttpRequest"说明就是ajax请求,需要特殊处理
if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
//告诉ajax我是重定向
response.setHeader("REDIRECT", "REDIRECT");
//告诉ajax我重定向的路径
response.setHeader("CONTENTPATH", basePath+"/login.html");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}else{
response.sendRedirect(basePath + "/login.html");
}
}
}
var jqxhr;
//设置ajax请求完成后运行的函数,
$.ajaxSetup({
complete:function(){
if("REDIRECT" == jqxhr.getResponseHeader("REDIRECT")){ //若HEADER中含有REDIRECT说明后端想重定向,
var win = window;
while(win != win.top){
win = win.top;
}
win.location.href = jqxhr.getResponseHeader("CONTENTPATH");//将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
}
}
});
补充:
普通转发
//类的注解不能使用@RestController,要用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public String test(@PathVariable String name) {
return "forword:/ceng/hello.html";
}
//类的注解可以使用@RestController,也可以使用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/ceng/hello.html").forward(request,response);
}
普通重定向
//类的注解不能使用@RestController,要用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public String test(@PathVariable String name) {
return "redirect:/ceng/hello.html";
}
//类的注解可以使用@RestController,也可以使用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET)
public void test(@PathVariable String name, HttpServletResponse response) throws IOException {
response.sendRedirect("/ceng/hello.html");
}
@RequestMapping("/save2")
public void save2(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException {
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}
部分内容转载自:https://blog.csdn.net/qq_30306577/article/details/71693301
https://www.cnblogs.com/lu51211314/p/10482099.html