• javaee springMVC session的使用


    controller

    package com.test.controller;
    
    import com.test.pojo.Address;
    import com.test.pojo.Users;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.CookieValue;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.SessionAttributes;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @RequestMapping("/users")
    //指定放入model中的某个变量 存入session中
    @SessionAttributes(value="sessionUser2")
    public class UsersController {
    
       
    
        //存入session方式1:原生session
        @RequestMapping("/getUser6")
        public String getUser6(HttpServletRequest request)
        {
            Users user=new Users(6,"daimenglaoshi6","888",new Address(1,"shanghai"),new Date(),888888);
    
            HttpSession session= request.getSession();
    
            session.setAttribute("sessionUser",user);
    
            return "showSessionUser";
        }
    
        //存入session方式2:用注解的方式  @SessionAttributes(value="sessionUser2")
        @RequestMapping("/getUser7")
        public ModelAndView getUser7() {
    
            ModelAndView modelAndView=new ModelAndView();
    
            Users user=new Users(7,"daimenglaoshi7","888",new Address(1,"shanghai"),new Date(),888888);
    
            modelAndView.addObject("sessionUser2",user);
    
            modelAndView.setViewName("showSessionUser2");
    
            return modelAndView;
        }
    
        @RequestMapping("/destroySession")
        public String destroySession(HttpServletRequest request){
    
              HttpSession session= request.getSession();
    
              //销毁session
              session.invalidate();
    
              return "showDestroySession";
        }
    
    
      
    
       
    
    
    
    
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    jsp

    <%--
      Created by IntelliJ IDEA.
      User: HIAPAD
      Date: 2019/12/5
      Time: 19:37
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Titletitle>
    head>
    <body>
    ${sessionScope.sessionUser.uname}<br/>
    
    ${sessionScope.sessionUser2.uname}<br/>
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    <%--
      Created by IntelliJ IDEA.
      User: HIAPAD
      Date: 2019/12/5
      Time: 19:08
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Titletitle>
    head>
    <body>
    ${sessionUser.uname}
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    <%--
      Created by IntelliJ IDEA.
      User: HIAPAD
      Date: 2019/12/5
      Time: 19:08
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Titletitle>
    head>
    <body>
    ${sessionScope.sessionUser2.uname}
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    两种方式存储session

    方式一

    原生方式

    方式二

    注解方式

    推荐

    推荐使用原生方式,因为经过测试,销毁方法只能销毁原生方式创建的session

  • 相关阅读:
    Knowledge Graph Prompting for Multi-Document Question Answering
    Vue.js 路由时用于提高应用程序性能
    【C++】加了<string.h>还是报“strlen:找不到标识符”的错误
    C++ 语言学习 day15 复习 (7)
    object类的一些方法
    react-route的路由
    21天学会C++:Day14----模板
    力扣第101题 c++ 递归 迭代 双方法 +注释 ~
    205.同构字符串
    从0手写两轮差速机器人urdf模型
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/132790967