如何跳转指定页面后再次跳转到另一页面或原来的页面

这里可以采用redirect以get形式向指定页面发出get请求,指定页面完成后会再次跳转到redirect后边指定的页面

(注意:这里的redirect只是一个自定义参数,并不是自带的,名称可以随便起,不一定要是redirect)

格式:需要请求的URL?redirect=请求URL完成后需要跳转的地址

例如:

response.sendRedirect("http://localhost:8080/test?redirect=http://localhost:8080/test2;

当然,这种用途常见于跳转到登陆页面登陆后再次返回原来的页面:

response.sendRedirect("http://localhost:8080/page/login?redirect=" + request.getRequestURL());

接下来,当请求登陆时,取出这个参数,这里以SpringMVC为例:

@RequestMapping("/page/login")
    public String showLogin(String redirect, Model model) {
     //把请求登陆后跳转的页面路径再次转发到登陆页面进行js处理
        model.addAttribute("redirect", redirect);
        return "login";
    }

详情解释在代码注释中

//登陆页面获取要重定向的地址
var redirectUrl = "${redirect}";

//根据自己情况当页面登陆成功时对重定向的地址进行跳转,登录失败就不进行跳转,下边代码表示如果没有追加redirect参数或为空就跳转到指定页面
//location.href = "http://localhost:8082
doLogin:function() {
                $.post("/user/login", $("#formlogin").serialize(),function(data){
                    if (data.status == 200) {
                        jAlert('登录成功!',"提示", function(){
                            if (redirectUrl == "") {
                                location.href = "http://localhost:8082";
                            } else {
                                location.href = redirectUrl;
                            }
                        });
                        
                    } else {
                        jAlert("登录失败,原因是:" + data.msg,"失败");
                    }
                });
            }

好了,这就做到了登陆后跳转到自己想要的页面,当然,这只是众多方法之一

发布了43 篇原创文章 · 获赞 12 · 访问量 4679

猜你喜欢

转载自blog.csdn.net/Jarbein/article/details/90741690