login/logout切换
2024-08-30 12:43:10
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";
}
最新文章
- 又一个半成品库 weblog rpc client
- MySQL(三)
- ASP.NET页面之间传递值的几种方式
- IO复用_epoll函数
- MongoDB学习笔记二:创建、更新及删除文档
- Top K Frequent Elements
- Swap Nodes &; Reverse Nodes in k-Group
- GC是什么? 为什么要有GC?
- [改善Java代码]覆写变长方法也循规蹈矩
- C#.NET开源项目、机器学习、商务智能
- PHP获取远程图片并调整图像大小
- 指纹增强程序Hong_enhancement
- bat加载和分离VHD
- Java垃圾回收总结
- [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第3讲(逻辑方法的实现)
- Bootstrap3 代码-代码块
- bootstrap模态框显示时被遮罩层遮住了
- Job for ssh.service failed because the control process exited with error code.......
- Android studio 一个项目引入另一个项目作为Libary
- redis简单使用
热门文章
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- linux截取字符串的多种方法
- eclipse中git插件配置 编辑
- w3c School
- kettle连接mysql
- HttpReceiveRequestEntityBody 使用应注意的地方
- matlab mse函数
- 第2章 熟悉Eclipse开发工具---- System.out.println(";sum=";+(a+b));
- C#第六天
- javascript显式类型的转换