【Java】前后端传参

2022/3/1 周二

٩(๑ ^ o ^ ๑)۶ 2022年第一篇博客~

开发中发现自己在前后端传参方面还是不太熟练,这篇博客是为了记录传递错误信息所用到的方法。如果有错误或者更好的实现方法,欢迎在评论区交流,十分感谢!


最近在研究企业微信通讯录导入若依后台管理系统,然后再提供扫码登录,遇到了一个问题:如果若依系统里没有这个用户,那么他用企业微信能扫码授权的话,若依这边匹配用户失败,就会重新跳转回登录页,这时我希望有一个弹窗效果来提示用户,如下:
提示

1. session

首先想到的就是后端在用户匹配失败时,将错误信息存入session:

HttpSession session = request.getSession();
session.setAttribute("errorMsg", errorMsg);

response.sendRedirect("/login");

前端在页面加载时弹窗:

var errorMsg = [[${session.errorMsg}]];

$(function() {
    // 如果是扫码登录跳转过来的,页面提示登录异常信息
    if(errorMsg != null){
        $.modal.alert("用户不存在!如需使用请联系系统管理员");
//        sessionStorage.removeItem("errorMsg");
//        sessionStorage.setItem("errorMsg",null);
//        sessionStorage.clear("errorMsg",null);
    }
});

但试了这三个删除session的方法,均没起作用(在开发者工具中并没有看到session,原来前端不能删除后端建的session)。这样的话,用户刷新界面,都会一直弹窗,体验感不太好。

另外还想到了可以在弹窗之后,用ajax调用后台接口去删除session,但是感觉为了一个错误信息弄这么麻烦不值得 … …

2. cookie

于是又想到用cookie存在客户端:

Cookie cookie = new Cookie("errorMsg",errorMsg);
response.addCookie(cookie);

从网上找了js获取和删除cookie的方法:

$(function() {
	var errorMsg = getCookie("errorMsg");
    // 如果是扫码登录跳转过来的,页面提示登录异常信息
    if(errorMsg != null){
        $.modal.alert("用户不存在!如需使用请联系系统管理员");
    }
});

function getCookie(name) {
    var prefix = name + "=";
    var start = document.cookie.indexOf(prefix);
    console.log("cookie: " + document.cookie);
    if (start == -1) {
        return null;
    }
    var end = document.cookie.indexOf(";", start + prefix.length);
    if (end == -1) {
        end = document.cookie.length;
    }
    var value = document.cookie.substring(start + prefix.length, end);
    return unescape(value);
}

function deleteCookie(name){
   var date=new Date();
   date.setTime(date.getTime()-10000);
   document.cookie=name+"=v; expire="+date.toGMTString();
}

因为重定向cookie会丢失,所以跳转到登录页面只能用转发的方式:

request.getRequestDispatcher("/login").forward(request,response);

而转发的话一是页面地址栏还停留在之前扫码到企业微信的url(但应该可以在弹窗之后再location.href跳转来解决),二是可能会有其他cookie来干扰(这里也有可能是我找的这段获取删除cookie的代码有问题?),总之还是放弃了用cookie这个想法。

3. attribute

还看到了request.setAttribute()的方法,但这不是用的JSP了,就没有考虑。

4. parameter和ModelMap

最后想到了重定向到login页面时,直接在url上带参数:

response.sendRedirect("/login?errorMsg="+errorMsg);

login接口里判断如果errorMsg不为空,就将其存到ModelMap里:

if(null != request.getParameter("errorMsg")){
    mmap.addAttribute("errorMsg",request.getParameter("errorMsg"));
}
return "login";

这样也不会影响正常的登录。

前端取值并弹窗:

var errorMsg = [[${errorMsg}]];

$(function() {
	// 如果是扫码登录跳转过来的,页面提示登录异常信息
    if(errorMsg != null){
        $.modal.alert("用户不存在!如需使用请联系系统管理员");
		$(".layui-layer-btn0").on("click",function(){
		    location.href = ctx + 'login';
		})
	}
});

最后加了一个弹窗之后的方法,在用户点击确定按钮(“.layui-layer-btn0”)后,跳转到不带参数的login页面,这样再刷新页面就不会每次都有错误信息弹窗了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值