1、当前浏览器url
http://localhost:8081/login?refer=http%3A%2F%2Flocalhost%3A8081%2Fmember%2Fuser%2FuserMessage%2Flist
2、通过js将当前url写入cookie
/**
* 将跳转的url写入cookie
*/
function addUrl(){
var href=window.location.href;
if(href.indexOf("refer")!=-1){
var str=decodeURIComponent($.trim(href.split("refer=")[1]));
$.cookie('callbackUrl', str,{path:'/'});
}
}3、后台解析url,执行跳转
//后台先从cookie取出url,执行解析并跳转
String callBack_url=null;
if(cookies!=null&&cookies.length>0){
for (Cookie cookie : cookies) {
if(cookie.getName().equals("callbackUrl")){
callBack_url=cookie.getValue();
continue;
}
}
}
if(callBack_url!=null){
callBack_url=URLDecoder.decode(callBack_url, "UTF-8");
response.sendRedirect(callBack_url);//直接跳转
}
本文介绍了一种在前端和后端实现URL操作的方法:首先使用JavaScript将当前页面的URL存入Cookie中;然后,在后端从Cookie读取URL,并进行解析及执行页面跳转。该方法适用于需要记录登录前页面地址并在登录后返回该页面的场景。
326

被折叠的 条评论
为什么被折叠?



