将数据存放在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} --%>

 

posted on 2018-09-27 20:46  爪哇玛咖  阅读(2147)  评论(0编辑  收藏  举报

导航