实现页面登录后仍然跳回当前页面

1 页面部分

 



   var ctx = "${pageContext.request.contextPath}";
            function downloadOpt(item){
  var param = {};
  param.revitId = $(item).data("id");
  param.toUrl = $.base64.encode(location.pathname + "?revitAddInId=" + $(item).data("id"));
  $.post(ctx + "/revit/getDownloadPluginLink.html", param, function(data) {
if(data.success){
location.href = data.data;
}else{
var errorMessage = data.message.split(":")[0];
error(errorMessage);
if (errorMessage.indexOf("登录") != -1) {
location = "${pageContext.request.contextPath}" + "/loginPersonal.html";
}
}
});
   
    }



2.拦截器实现

//下载时候要登录
public class RevitInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(RevitInterceptor.class);


private @Autowired RequirementService requirementService;


@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String shortUri = ServletUtils.getShortUri(request);
OnlineUser user = SessionHelper.getOnlineUser(request.getSession());
HttpSession session = request.getSession();
String toUrl = null;


if (shortUri.contains("list.html") || shortUri.contains("details.html")||shortUri.contains("authorInfo.html")
|| shortUri.contains("getPoolMoney.html")|| shortUri.contains("getPersonMoney.html")||shortUri.contains("getBetResult.html")) {
//Long reqId = null;
try {

} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return true;
}


if (null == user) {
toUrl = request.getParameter("toUrl");//获得登陆后回跳地址
if (isAsyncRequest(request)) {
if (toUrl != null) {
session.setAttribute("toUrl", new String(Base64.decodeBase64(toUrl)));//回跳地址写入session
}
response.setContentType("application/json");
Writer.write(response, Ajax.buildErrorResult("您没有登录,请先登录!"));
} else {
if (toUrl != null) {
session.setAttribute("toUrl", toUrl);
} else {
session.setAttribute("toUrl", request.getRequestURI() + "?" + request.getQueryString());
}
response.sendRedirect(request.getContextPath() + RedirectURL.LOGIN_PERSONAL);
}
return false;
}


return true;
}


private boolean isAsyncRequest(HttpServletRequest request) {
return request.getHeader("X-Requested-With") != null;
}

}


3.先跳到登录控制器

@RequestMapping(value = "loginPersonal")
public ModelAndView loginDesign(HttpSession session) {
ModelAndView view = new ModelAndView();
String toUrl = (String) session.getAttribute("toUrl");
view.addObject("toUrl", toUrl); //再把url 放到页面 hidden
view.setViewName("/user/loginDesign");
return view;
}


4


class="revitbus-init-page-box-personal personal-enterprise ">











js登录 跳转tourl

function onLogin(){
var code = $("input[name='code']").val();
var pwd = $("input[name='password']").val();
var userName = $("input[name='username']").val();
if(userName.length == 0){
info("请输入账号");
return;
}
if(pwd.length == 0){
info("请输入密码");
return;
}
if(code.length == 0){
info("请输入验证码");
return;
}
var url = '/revitbus/login/personal.html';
var param = {};
param['username'] = userName;
param['password'] = hex_md5(pwd);
param['code'] = code;
var toUrl = $("input[name='toUrl']").val();
var forward = "/revitbus/welcome.html";
if(!(toUrl == "")){
if(toUrl != forward){
forward = toUrl;
}
}
$.post(url,param,function(data) {
if (data.success) {
window.location.href = forward;
} else {
console.log(data);
error(data.message);
}
});
}


另外一种方法 前2步都一样

再加上以下内容 

在加一个登录的拦截器 :类似于第二步

拦截下 tourl 属性 有的话 跳转 下面大致写下 没有验证 做一个重定向


在类似以下方法中

  1.     /** 
  2.      * 在业务处理器处理请求执行完成后,生成视图之前执行的动作    
  3.      * 可在modelAndView中加入数据,比如当前时间 
  4.      */  
  5.     @Override    
  6.     public void postHandle(HttpServletRequest request,    
  7.             HttpServletResponse response, Object handler,    
  8.             ModelAndView modelAndView) throws Exception {     
  9.         log.info("==============执行顺序: 2、postHandle================");    
  10.         if(modelAndView != null){  //加入当前时间    
  11.             modelAndView.addObject("var""测试postHandle");    
  12.         }    
  13.     }

加入以下内容


if (null == user) {
toUrl = request.getParameter("toUrl");
if (isAsyncRequest(request)) {
if (toUrl != null) {
session.setAttribute("toUrl", new String(Base64.decodeBase64(toUrl)));
}
response.setContentType("application/json");
response.sendRedirect(toUrl);  //可以试试 应该可以 大致思路这样
} else {
.................
}
return false;
}


你可能感兴趣的:(jsp)