您的位置:首页 > 其它

web开发,登录后跳转到之前页面

2014-09-11 09:55 323 查看
点击登录,弹出对话框,触发memberlogin():
//用户登录
function memberlogin(){
var href=window.location.href;
if(href.indexOf("login")!=-1){
window.location.href = href;
}else{
href = href.substring(href.lastIndexOf("/")+1);
href = encodeURIComponent(href);//函数可把字符串作为 URI 组件进行编码。
window.location.href = "/mall/login.htm?rPath="+href;//跳转到登录页面

}
return;


点击登录后,触发login()

function login(){
var loginName=$("#loginName").val();
var pwd=$("#pwd").val();
if(loginName=="登录账号" || loginName==""){
alertDialog("请输入登录账号");
return;
}
if(pwd=="登录密码" ||pwd==""){
alertDialog("请输入登录密码");
return;
}
if(pwd.indexOf(' ')>=0){
new Dialog('<div style="width:280px; height:100px;line-height:100px;text-align:center;">输入的密码有空格,请检查.</div>',{noneborder:0,beforeClose:function(){$("#pwd").focus();return true;}}).show();
return false;
}
$.post("/mall/checkCustomerNameAndPassword_Ajax.htm",{"accountName":loginName,"password":pwd},function(data){
if(data =="success"){
$("#loginForm").submit();
//                    $("form#validateSubmitForm").submit();
}
else{
alertDialog("登录名或密码错误!");
}
});
}

//支持enter键登录
document.onkeydown = function(e){
var e = window.event   ?   window.event   :   e;
if(e.keyCode == 13){
var btn=document.getElementById("btnLogin");
btn.onclick();
}
}
验证通过,提交表单

@RequestMapping(value = "doLogin", method = RequestMethod.POST)
public String doLogin(@ModelAttribute("customer") Customer customer, BindingResult result, ModelMap modelMap,
HttpSession session, Locale locale,HttpServletRequest request,HttpServletResponse response) {
String accoutName = customer.getAccountName();
if (StringUtils.isBlank(accoutName)) {
FieldError fieldError = new FieldError("customer", "accoutName", accoutName, true, null, null, messageSource.getMessage(
"customer.accoutName.require", null, locale));
result.addError(fieldError);
modelMap.put("msg", "请输入用户");
return login(customer, result, modelMap,request,session);
}
Customer oUser = customerService.login(accoutName, customer.getPassword());
if (oUser == null) {
FieldError fieldError = new FieldError("customer", "accoutName", accoutName, true, null, null, messageSource.getMessage(
"customer.accoutName.notexist", null, locale));
result.addError(fieldError);
modelMap.put("msg", "用户名或密码错误");
return login(customer, result, modelMap,request,session);
}
if (!oUser.getPassword().equals(customer.getPassword())) {
FieldError fieldError = new FieldError("customer", "password", customer.getPassword(), true, null, null,
messageSource.getMessage("customer.password.error", null, locale));
result.addError(fieldError);
modelMap.put("msg", "密码错误");
return login(customer, result, modelMap,request,session);
}
//添加本地购物车内容
String cookieCart=getCookie(WebConstants.COOKIE_CART,request);
if(LogicUtils.isNotNullAndEmpty(cookieCart)){
try {
String[] cookieCarts=cookieCart.split("_");
List cs=new ArrayList<Integer>();
for(int i=0;i<cookieCarts.length;i++){
String item=cookieCarts[i];
if(LogicUtils.isNotNullAndEmpty(item)){
cs.add(Integer.parseInt(item));
}
}
cartService.addShoppingCarts(cs, (Customer) oUser);
deleteCookie(WebConstants.COOKIE_CART,response);
} catch (Exception e) {
log.info("用户登陆,购物车更新失败",e);
}
}
Customer cus = customerService.findByAccountName(oUser.getAccountName());
session.setAttribute(WebConstants.LOGIN_SESSION, cus);
session.removeAttribute(WebConstants.LOGIN_STATU);
String rPath=(String) session.getAttribute(WebConstants.LOGIN_REDIRECT);
if(LogicUtils.isNotNullAndEmpty(rPath)
&& !rPath.contains("logout")
&& !rPath.contains("register")
&& !rPath.contains("main.html")){
session.removeAttribute(WebConstants.LOGIN_REDIRECT);
try {
rPath= new String(rPath.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(),e);
return "main";
}
return "redirect:"+rPath;
}
return "main";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: