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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM