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";
}

最新文章

  1. 又一个半成品库 weblog rpc client
  2. MySQL(三)
  3. ASP.NET页面之间传递值的几种方式
  4. IO复用_epoll函数
  5. MongoDB学习笔记二:创建、更新及删除文档
  6. Top K Frequent Elements
  7. Swap Nodes &amp; Reverse Nodes in k-Group
  8. GC是什么? 为什么要有GC?
  9. [改善Java代码]覆写变长方法也循规蹈矩
  10. C#.NET开源项目、机器学习、商务智能
  11. PHP获取远程图片并调整图像大小
  12. 指纹增强程序Hong_enhancement
  13. bat加载和分离VHD
  14. Java垃圾回收总结
  15. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第3讲(逻辑方法的实现)
  16. Bootstrap3 代码-代码块
  17. bootstrap模态框显示时被遮罩层遮住了
  18. Job for ssh.service failed because the control process exited with error code.......
  19. Android studio 一个项目引入另一个项目作为Libary
  20. redis简单使用

热门文章

  1. Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
  2. linux截取字符串的多种方法
  3. eclipse中git插件配置 编辑
  4. w3c School
  5. kettle连接mysql
  6. HttpReceiveRequestEntityBody 使用应注意的地方
  7. matlab mse函数
  8. 第2章 熟悉Eclipse开发工具---- System.out.println(&quot;sum=&quot;+(a+b));
  9. C#第六天
  10. javascript显式类型的转换