1. 前端按钮

 <img border="0" width="18" height="18" src="<%=basePath %>/style/images/top/user.png" /> <font style="font-size:11px; font-family:Microsoft YaHei"> ${usersession.name }
<c:forEach items="${usersession.roles }" var="r">
${r.name }
</c:forEach>
</b></font> <%if(session.getAttribute("usersession")!=null){%>
<input type="button" value="Log Out" onclick="javascript:logout()" style="color:#2c3e50; font-size:12px;font-weight:bold; border-radius:3px; vertical-align:middle;height:20px; width:70px; "/>
<%}else {%>
<input type="button" value="Log In" onclick="javascript:login()" style="color:#2c3e50; font-size:12px;font-weight:bold; border-radius:3px; vertical-align:middle;height:20px; width:70px; "/>
<%} %>

2. js函数

<script>
function logout(){
$.getJSON("/portal/user/logout?rand="+Math.random(),function(data){
if("success"==data.result){
parent.location.href="/portal/home/index";
}
else{
alert("logout failure!");
}
});
}
function login(){
window.open("/portal/user/loginUI","_parent");
}
</script>

3. controller端实现

@RequestMapping("/login")
public String login(String loginName, String password, HttpSession session,HttpServletRequest request){
//session.invalidate();
User user = userService.findByLoginNameAndPassword(loginName, password);
if(user == null){
request.setAttribute("loginError", "用户名或者密码错误");
return "/userController/loginUI";
}
else{
session.setAttribute("usersession", user);
}
return "/homeController/index";
} @RequestMapping("/logout")
public String logout( HttpSession session,HttpServletResponse response){
session.removeAttribute("usersession");
session.invalidate();
JSONObject data = new JSONObject();
try {
data.put("result", "success");
} catch (Exception e) {
System.out.println(e.getMessage());
} PrintWriter out = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
try {
out=response.getWriter();
out.write(data.toString());
out.flush();
out.close();
return null;
} catch (IOException e) {
e.printStackTrace();
}
//return "/userController/logout";
return "/homeController/index";
}

login/logout切换的更多相关文章

  1. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

  2. [Node] Stateful Session Management for login, logout and signup

    Stateful session management: Store session which associate with user, and store in the menory on ser ...

  3. FaceBook登陆API -- Login with API calls

    Login with API calls Related Topics Understanding sessions FBSession Error handling FBError FBLoginC ...

  4. Yarn 和 Npm 命令行切换 摘录

    原文作者: @Gant Laborde原文地址: https://shift.infinite.red/np...中文翻译: @文蔺译文地址:http://www.wemlion.com/2016/n ...

  5. Angular--页面间切换及传值的四种方法

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  6. Spring Security笔记:使用数据库进行用户认证(form login using database)

    在前一节,学习了如何自定义登录页,但是用户名.密码仍然是配置在xml中的,这样显然太非主流,本节将学习如何把用户名/密码/角色存储在db中,通过db来实现用户认证 一.项目结构 与前面的示例相比,因为 ...

  7. [转]Angular: Hide Navbar Menu from Login page

    本文转自:https://loiane.com/2017/08/angular-hide-navbar-login-page/ In this article we will learn two ap ...

  8. Roundcube login via PHP script

    目前正在整合 roundcube 1.0.5 的邮件系统和其他系统,想取消登录过程,发现了这个,先赞一个! 原文地址: http://blog.philippheckel.com/2008/05/16 ...

  9. [Firebase] 3. Firebase Simple Login Form

    Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...

随机推荐

  1. Linux的文件/目录的权限

    文件类型 权限 属主 属组 目录名/文件名drwx------ 2 alice girls 4096 9月 25 11:33 alicedrwx------ 2 rose girls 4096 9月 ...

  2. 12款免费与开源的NoSQL数据库

    Naresh Kumar是位软件工程师与热情的博主,对于编程与新事物拥有极大的兴趣,非常乐于与其他开发者和程序员分享技术上的研究成果.近日,Naresh撰文谈到了12款知名的免费.开源NoSQL数据库 ...

  3. Javaweb 第7天 Servlet课程

    Servlet课程 三日大纲 ● 网络概念,专业术语 ● Tomcat使用,发布网站,使用Myeclispe发布网站(搭建环境) ● 编写Servlet,Servlet生命周期 ● 用户注册,显示所有 ...

  4. RabbitMQ高可用配置(Haproxy + Keepalived)

    网络结构如下图: 共有104.105.106三台RabbitMQ Server,互为集群 其中104和105安装了Haproxy,每个Haproxy承担三台RabbitMQ server的负载均衡 两 ...

  5. 2016年团体程序设计天梯赛-决赛 L1-3. 出租(20)

    下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对 ...

  6. 【Sort】多种排序

    这篇文章包含了插入排序,希尔排序,堆排序,归并排序和快速排序,是前几篇文章的集合. 一共包括三个文件 sort.h sort.cpp main.cpp 1.main.cpp #include < ...

  7. AVL树 高度平衡的二叉查找树

    1.What is AVL tree? AVL tree 是一种特殊的二叉查找树,,首先我们要在树中引入平衡因子balance,表示结点右子树的高度减去左子树的高度差(右-左),对于一棵AVL树要么它 ...

  8. IOS真机Profile时调用树中的对象只是显示地址,没有显示symbol name

    解决问题的办法: 1.确认工程设置中的Scheme,profile选项对应的是debug版本还是release版本 2.确认工程设置中debug版本或者release版本是否生成了符号表 " ...

  9. webapi中的路由前缀

    Route Prefixes Often, the routes in a controller all start with the same prefix. For example: public ...

  10. C++中##(两个井号)和#(一个井号)用法

    C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于编译期概念(而非运行期概念).下面对常遇到的宏的使用问题做了简单总结.关 于#和##在C语言的宏中,#的功能是将其后面的宏参数进行字符串化 ...