栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC:Session作用域和Application作用域

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC:Session作用域和Application作用域

 

dispatcher.xml:




    
    
    

    

    
    
        
        
    

View3Controller:

package com.baizhiedu;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/view3")
//@SessionAttributes(value = "name") //通过注解,让springMVC知道在Model存放的数据不是request作用域,而是Session作用域
@SessionAttributes(value = {"name","sex"})
public class View3Controller {
    @RequestMapping("/view1")
    public String view1(Model model){
        model.addAttribute("sex","female");
        model.addAttribute("age",10); //没有往Session中添加,默认存在Request作用域里面
        model.addAttribute("name","xioajr");
        return "result2";
    }
}

 result2.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/6
  Time: 10:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


  session attribute is ${sessionScope.name}

  request is attribute is ${requestScope.age}


result2.jsp:书写代码看看能否获取?

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/6
  Time: 10:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


    Title
    


  session attribute is ${sessionScope.name}

<%--  Model往Session中存储的数据,也会在Request中存储--%>
  request attribute is ${requestScope.name}
  
  request is attribute is ${requestScope.age}


 

 View3Controller:

package com.baizhiedu;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/view3")
//@SessionAttributes(value = "name") //通过注解,让springMVC知道在Model存放的数据不是request作用域,而是Session作用域
@SessionAttributes(value = {"name","sex"})
public class View3Controller {
    @RequestMapping("/view1")
    public String view1(Model model){
        model.addAttribute("sex","female");
        model.addAttribute("age",10); //没有往Session中添加,默认存在Request作用域里面
        model.addAttribute("name","xioajr");
        //return "result2";
        return "forward:/view3/view2";
    }

    @RequestMapping("/view2")
    public String view2(SessionStatus sessionStatus){
        if (!sessionStatus.isComplete()){
            sessionStatus.setComplete();//清空Session
        }
        return "result2";
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号