zoukankan      html  css  js  c++  java
  • SpringMVC(十六) 处理模型数据之SessionAttributes

    @SessionAttributes原理

      默认情况下Spring MVC将模型中的数据存储到request域中。当一个请求结束后,数据就失效了。如果要跨页面使用。那么需要使用到session。而@SessionAttributes注解就可以使得模型中的数据存储一份到session域中。

    @SessionAttributes参数

      1、names:这是一个字符串数组。里面应写需要存储到session中数据的名称。

      2、types:根据指定参数的类型,将模型中对应类型的参数存储到session中

        3、value:其实和names是一样的。

    Controller参考代码:

      

    复制代码
    package com.tiekui.springmvc.handlers;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.tiekui.springmvc.pojo.Address;
    import com.tiekui.springmvc.pojo.User;
    
    
    //http://www.cnblogs.com/caoyc/p/5635914.html
    //只要是types中定义的类型,都会自动加入到sessionAttributes中。@SessionAttributes注解用于在类修饰中,而不是方法
    @org.springframework.web.bind.annotation.SessionAttributes(value={"user"},types={Integer.class})
    @Controller
    public class SessionAttributes {
    
        @RequestMapping("testSessionAttributes")
        public String testSessionAttributes(Map<String, Object> map) {
            
            User userTk = new User();
            Address address = new Address();
            address.setCity("city");
            address.setProvince("province");
            userTk.setAge(19);
            userTk.setEmail("zhoutiekui@huawei.com");
            userTk.setPassword("test");
            userTk.setUsername("zhoutiekui");
            userTk.setAddress(address);
            
            map.put("user", userTk);
            map.put("age", 18);
            map.put("count", 30);
            
            return "testSessionAttributes";
        }
    }
    复制代码

    返回数据视图:

    复制代码
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
        sessionAtrributes: ${sessionScope.user}
        sessionAtrributes: ${sessionScope.age}
        sessionAtrributes: ${sessionScope.count}
    
    </body>
    </html>
    复制代码

    调用视图:

    <a href="testSessionAttributes">testSessionAttributes video 16</a>

    本例中的添加到map的age和count都没有在SessionAtrributes的names/value值中,但是仍然可以被视SessionAttributes.

    https://github.com/godmaybelieve
  • 相关阅读:
    bobobrowse为Lucene添加分组统计
    实现lucene检索结果排序
    facets in lucene
    lucene3.5 example
    Lucene聚类分组统计功能(grouping)
    Eclipse开发struts完全指南(三)实战
    缓存是什么?占内存吗?
    []利用memcached在多台服务器之间共享PHP的session数据
    HTML meta refresh 刷新与跳转(重定向)页面
    [置顶] 微信开发出现“该公众号暂时无法提供服务,请稍后再试”的坑
  • 原文地址:https://www.cnblogs.com/yuyu666/p/10136092.html
Copyright © 2011-2022 走看看