将数据存放在session里--小Demo

Posted coisini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数据存放在session里--小Demo相关的知识,希望对你有一定的参考价值。

在jsp页面,session得到的默认是Object类型的,所以需要自己toString,或者做其他的强制类型转换
package com.www.controller;

import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aaa.entity.UserInfo;


@Controller
//@Scope("singleton")
@Scope(value="prototype")
@RequestMapping("/hello")//窄化请求
public class HelloWorldController {
    private static int a=0;//静态
    private  int b=0;//非静态
    
    @RequestMapping("/chenjian")
    public  ModelAndView HelloTeacherChen() {
        System.out.println("访问到了!");
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.setViewName("teacher");
        return mv;

    }
    @RequestMapping("/HelloTeacherChenParam")
    public  ModelAndView HelloTeacherChenParam(UserInfo user,HttpSession session) {
        
        //System.out.println("访问到了!"+user.getName()+"年龄:"+user.getAge());
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.addObject("user", user);//modelandView默认存在request域
        session.setAttribute("user", user);
        mv.setViewName("teacher");
        return mv;
        
    }
    @RequestMapping("/HelloTeacherChenParamString")
    public  String HelloTeacherChenParamString(UserInfo user,HttpSession session) {
        
        //System.out.println("访问到了!"+user.getName()+"年龄:"+user.getAge());
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.addObject("user", user);//modelandView默认存在request域
        session.setAttribute("user", user);
    /*    mv.setViewName("teacher");*/package com.aaa.controller;

import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aaa.entity.UserInfo;


@Controller
//@Scope("singleton")
@Scope(value="prototype")
@RequestMapping("/hello")//窄化请求
public class HelloWorldController {
    private static int a=0;//静态
    private  int b=0;//非静态
    
    @RequestMapping("/chenjian")
    public  ModelAndView HelloTeacherChen() {
        System.out.println("访问到了!");
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.setViewName("teacher");
        return mv;

    }
    @RequestMapping("/HelloTeacherChenParam")
    public  ModelAndView HelloTeacherChenParam(UserInfo user,HttpSession session) {
        
        //System.out.println("访问到了!"+user.getName()+"年龄:"+user.getAge());
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.addObject("user", user);//modelandView默认存在request域
        session.setAttribute("user", user);
        mv.setViewName("teacher");
        return mv;
        
    }
    @RequestMapping("/HelloTeacherChenParamString")
    public  String HelloTeacherChenParamString(UserInfo user,HttpSession session) {
        
        //System.out.println("访问到了!"+user.getName()+"年龄:"+user.getAge());
        ModelAndView mv = new ModelAndView();
        mv.addObject("key", "I love you!");
        mv.addObject("user", user);//modelandView默认存在request域
        session.setAttribute("user", user);
    /*    mv.setViewName("teacher");*/
        return "teacher";
    }
    @RequestMapping("/HelloTeacherChenRedirectToMethod")
    public  String HelloTeacherChenRedirectToMethod(UserInfo user,HttpSession session) {
        System.out.println("访问到了!一会就重定向到HelloTeacherChen");
        return "redirect:/hello/chenjian.do";
    }
    @RequestMapping("/testSingleton")
    public void testSingleton() {
        System.out.println(a++ +"||"+b++);
    }

}

        return "teacher";
    }
    @RequestMapping("/HelloTeacherChenRedirectToMethod")
    public  String HelloTeacherChenRedirectToMethod(UserInfo user,HttpSession session) {
        System.out.println("访问到了!一会就重定向到HelloTeacherChen");
        return "redirect:/hello/chenjian.do";
    }
    @RequestMapping("/testSingleton")
    public void testSingleton() {
        System.out.println(a++ +"||"+b++);
    }

}


-------------jsp-----
   This is my JSP page!!!!!. <br>
    <%--  ${key}<br> --%>
<%--      name:${user.name}
     age:${user.age} --%>
     <%-- requestname:<%out.print(((UserInfo)request.getAttribute("user")).getName()); %> --%>
     <%-- sessionname:<%out.print(((UserInfo)session.getAttribute("user")).getName()); %> --%>
    <%--  age:${user.age} --%>

 

以上是关于将数据存放在session里--小Demo的主要内容,如果未能解决你的问题,请参考以下文章

单点登录demo

小demo总结

验证码

java集合存放引用数据类型的小问题

Android课程---Android Studio使用小技巧:提取方法代码片段

微信小程序-获取用户信息和openid,session_key,