zoukankan      html  css  js  c++  java
  • spring boot 之session

    1.登录之后跳转公告界面,没有登录的时候不能跳转

    SpringBoot+thymeleaf,在html页面获取session

    1.Controller层代码
    
    
     @RequestMapping("userLogin")
    .     public String userLogin(@RequestParam("userName")String userName,@RequestParam("password")String password,HttpServletRequest request) {
             int n=userService.userLogin(userName, password);
             if(n==1) {
                 HttpSession session=request.getSession();//获取session并将userName存入session对象
                 session.setAttribute("userName", userName);
                 return "user/index";
             }
            
            return "index";
         }
    
    
    2.View层
    
    
    <!DOCTYPE html>
     <html xmlns:th="http://www.thymeleaf.org">
     <head>
     <meta charset="UTF-8">
     <title>Insert title here</title>
     </head>
     <body>
     登陆成功
     <div th:text="${session.userName}"></div> <!--获取session中的userName -->
     </body>
     </html>
     

    以上的方法并没有用到,计划使用的是页面js判断,若account不为空则不使其跳转
    值得注意的是:

    在thymeleaf框架中,在js代码中,去获取到session中的属性值。
    
    一、内联js
    
    <script th:inline="javascript">
    
    //代码
    
    </script>
    
    二、
    
    
    <script th:inline="javascript">
    
    /*<![CDATA[*/
    
    var xx=/*[[${session.xx}]]*/   --获取session中的值
    
    var yy=/*[[${yy}]]*/      --获取model中的值
    
    /*]]>*/
    
    </script>
    所以自己设计的代码是:
    controller层:

    @RequestMapping(value="/gg")
    public String login_gg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("================login_gg===================");
        System.out.println(account.getUsername());
        request.getSession().setAttribute("accountName", account.getUsername());
        return "gg";
    }
    HTML层:

    <script th:inline="javascript">
    
              function toAccount(){
                  var accountName = [[${session.accountName}]];
                  if( accountName != null) {
    
                          layer.alert("已经成功登录", {
                              skin: 'layui-layer-lan'
                              , closeBtn: 0
                              , anim: 4 //动画类型
                          });
          }
              }
    
    </script>
     

    2.实现登录之后不能再进入“我的账户”界面
    3.实现退出功能
    技术点一:在layer中添加一个按钮,并且按钮绑定链接

    layer.alert("已经成功登录用户"+accountName, {
        btn: ['退出登录','确认'],
        skin: 'layui-layer-lan'
        , closeBtn: 0
        , anim: 4 //动画类型
        ,success: function(layero){
            var btn = layero.find('.layui-layer-btn');
            btn.find('.layui-layer-btn0').attr({
                href: '/visit'
                ,target: '_blank'
            });
    技术点二:在layer中按钮绑定一个事件,格式为btn.find('按钮id').click(function(){自定义事件代码});

    btn.find('.layui-layer-btn0').click(function(){  var userAgent = navigator.userAgent;
        if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {
            window.location.href="about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }});
    此页面绑定的是关闭当前页面的代码

    if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {
        window.location.href="about:blank";
        window.close();
    } else {
        window.opener = null;
        window.open("", "_self");
        window.close();
    }
  • 相关阅读:
    python3图片转化成字符画
    ubuntu 18.04安装PIL(Python Imaging Library )
    Ubuntu 18.04安装钉钉
    django 使用iframe跨域请求
    django 自定义日志字段
    Ubuntu18.04下安装搜狗输入法(亲测有效)
    Nginx 配置指令手册
    js闭包Demo
    自己写了一个无缝滚动的插件(jQuery)
    写JQuery 插件 什么?你还不会写JQuery 插件
  • 原文地址:https://www.cnblogs.com/wf1647790534/p/9802329.html
Copyright © 2011-2022 走看看