获取返回URL

2020-04-28  本文已影响0人  叶小溪

Java设置Cookie

/**
 * 获取返回URL
 *
 * @param request
 * @param response
 */
public static void gerReturnUrl(HttpServletRequest request, HttpServletResponse response) {
    try {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            stringBuffer.append("&");
            stringBuffer.append(stringEntry.getKey());
            stringBuffer.append("=");
            stringBuffer.append(URLEncoder.encode(stringEntry.getValue()[0], "UTF-8"));
        }
        String url = CommonConstants.contextPath+request.getRequestURI();
        if (parameterMap.size() > 0) {
            url = url + stringBuffer.replace(0, 1, "?");
        }
        WebUtils.setCookie(response, "reUrl", url, 1);
    }catch (Exception e){
        logger.error("returnUrl",e);
    }
}

/**
 * 增加或修改cookie
 * 
 * @param response
 * @param key
 * @param value
 * @param days
 */
public static void setCookie(HttpServletResponse response, String key, String value, int days) {
    setCookie(response, key, value, days, MYDOMAIN);
}

/**
 * 增加或修改cookie
 * 
 * @param response
 * @param key
 * @param value
 * @param days
 */
public static void setCookie(HttpServletResponse response, String key, String value, int days, String domain) {
    if (key != null && value != null) {
        Cookie cookie = new Cookie(key, value);
        // 设置有效日期
        cookie.setMaxAge(days * 24 * 60 * 60);
        // 设置路径(默认)
        cookie.setPath("/");
        if (StringUtils.isNotEmpty(domain)) {// domain != null
            cookie.setDomain(domain);
        }
        // 把cookie放入响应中
        response.addCookie(cookie);
    }
}

Java获取Cookie

/**
 * 获取Cookie中保存的返回地址
 * @param request
 * @return
 */
public static String returnUrl(HttpServletRequest request, String url) {
    try {
        // 返回原来的页面
        String reUrl=WebUtils.getCookie(request,"reUrl");
        if (StringUtils.isNotBlank(reUrl)){
            return "redirect:"+reUrl;
        }
    }catch (Exception e){
        logger.error("returnUrl",e);
    }
    return "redirect:"+CommonConstants.contextPath + url;
}

/**
 * 得到指定键的值
 *
 * @param request
 * @param key
 *            指定的键
 * @return String 值
 */
public static String getCookie(HttpServletRequest request, String key) {
    Cookie[] cookies = request.getCookies();
    String resValue = "";
    if (cookies != null) {
        if (cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                if (key.equalsIgnoreCase(cookies[i].getName())) {
                    if (StringUtils.isNotEmpty(cookies[i].getValue())) {
                        resValue = cookies[i].getValue();
                    }
                }
            }
        }
    }
    return resValue;
}

Js获取Cookie

// 自动跳转
function fanhui(url){
    var reUrl = getCookieFromServer("reUrl")
    if (!isEmpty(reUrl)) {
        location.href=reUrl;
    }else{
        location.href=baselocation + url;
    }
}

// 手动跳转
function fanhuiManual(url){
    var reUrl = getCookieFromServer("reUrl")
    if (!isEmpty(reUrl)) {
        return reUrl;
    }else{
        return baselocation + url;
    }
}

/**
 * 获取Cookies方法,解决中文乱码
 * @param cookieName
 * @returns
 */
function getCookieFromServer(cookieName) {
    var cookieString = document.cookie;
    var start = cookieString.indexOf(cookieName + '=');
    // 加上等号的原因是避免在某些 Cookie 的值里有
    // 与 cookieName 一样的字符串。
    if (start == -1) // 找不到
        return null;
    start += cookieName.length + 1;
    var end = cookieString.indexOf(';', start);
    if (end == -1) {
        return Url.decode(cookieString.substring(start));
    } else {
        return Url.decode(cookieString.substring(start, end));
    }
}

var Url = {
    encode: function (string) {
        return escape(this._utf8_encode(string));
    },
    decode: function (string) {
        return this._utf8_decode(unescape(string));
    },
    _utf8_encode: function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12)
                    | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    }
};
项目地址
CommonConstants.contextPath; //http://127.0.0.1

Java方法调用

return WebUtils.returnUrl(request,"/admin/quest/toQuestionList");

Js方法调用

onclick="fanhui('/admin/course/courseList')"
上一篇 下一篇

猜你喜欢

热点阅读