我在Action方法中写入代码
Map map=new HashMap<String, Object>();
Map map1=(Map<String, Object>) ActionContext.getContext().getSession().put("user", getUsername());
map.put("user", getUsername());
在debug模式中看到,map1=null,map正常。
ActionContext.getContext().getSession()不是返回一个map类型吗?
我相当于用ActionContext.getContext().getSession()实例化一个map1,并且存入东西。我并不关心它在session方面的功能,
我这样理解有错吗?

解决方案 »

  1.   

    ActionContext.getContext().getSession()不是返回一个map类型吗?教你一个办法 鼠标的箭头放到getSession()上你会看到 ActionContext.getContext().getSession()的返回值事什么类型的
      

  2.   

    getSession()返回的是一个Map
    改成这样:
    Map map1=(Map<String, Object>) ActionContext.getContext().getSession();
    map1.put("user",getUsername());
      

  3.   

    你最后调用的是put方法,其实Map map1接受的是put的返回值,而不是getsession的返回值,put方法的返回值是void我记得。所以导致了你的map1是null
      

  4.   

    ActionContext.getContext().getSession()是返回一个map,但ActionContext.getContext().getSession().put("user", getUsername());不是啊。
    你这样:
    Map map1=(Map<String, Object>) ActionContext.getContext().getSession();
    map1.put("user", getUsername());
      

  5.   

    我告你吧,其实put返回类型是put()中vlaue的类型。比如map.put("A","a"),
    Class o=map.put("A","a").getClass()将会是个 java.lang.String
    但是你不能 String o1= map1.put("user", user);这样编译不会通过,可见其实put返回的应该是个泛型,只有强制转换String o1=(String) map1.put("user", user);这样才能行。
    jdk中也是这样定义的V put(K key,V value)
      

  6.   

    ActionContext.getContext().getSession().getClass(),返回是class org.apache.struts2.dispatcher.SessionMap。
    怎样判断 class org.apache.struts2.dispatcher.SessionMap是map的子类?
      

  7.   

    struts2么,建议用ServletActionContext.getRequest().getSession()。。
      

  8.   

    既然得需要强制转换,证明ActionContext.getContext().getSession();其实并不是正真的map类型。
      而却Boolean o9=ActionContext.getContext().getSession().getClass().isInstance(Map.class)
    o9是false,
    怎么办
      

  9.   

    重点出来了
    public class SessionMap<K, V> extends AbstractMap<K, V> implements Serializable。
    public interface Map<K,V>
    public abstract class AbstractMap<K,V>extends Object implements Map<K,V>
    刚发现,原来Map是个接口
      

  10.   


    文档里面是这样描述put的返回的:
    返回:
    以前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null。(如果该实现支持 null 值,则返回 null 也可能表示此映射以前将 null 与 key 关联)。 所以返回的是value,不是你的map。除非它本身value里存的是map。
      

  11.   


    楼主玩脱了,先看看基础滴把
    你想要往session里赋值
    7楼:
    struts2么,建议用ServletActionContext.getRequest().getSession()
    你的这个写法错的是基础
    3楼:
    你最后调用的是put方法,其实Map map1接受的是put的返回值,而不是getsession的返回值,put方法的返回值是void我记得。所以导致了你的map1是null
    楼主加油。
      

  12.   

    另外正如我说的put方法返回的是Value的类型,不是void
      

  13.   

    Map map1=(Map<String, Object>) ActionContext.getContext().getSession().put("user", getUsername());
    这句代码能编译过???
      

  14.   

    你是要把数据放在session范围内吗?ServletActionContext.getRequest().getSession();
      

  15.   

    还没结啊???
    Map map1=(Map) ActionContext.getContext().getSession();
    map1.put("user", getUsername());
    有返回值你这么写不就得了么?
    还有你说的返回值
        /**
         * Associates the specified value with the specified key in this map
         * (optional operation).  If the map previously contained a mapping for
         * this key, the old value is replaced by the specified value.  (A map
         * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
         * if {@link #containsKey(Object) m.containsKey(k)} would return
         * <tt>true</tt>.)) 
         *
         * @param key key with which the specified value is to be associated.
         * @param value value to be associated with the specified key.
         * @return previous value associated with specified key, or <tt>null</tt>
         *        if there was no mapping for key.  A <tt>null</tt> return can
         *        also indicate that the map previously associated <tt>null</tt>
         *        with the specified key, if the implementation supports
         *        <tt>null</tt> values.
         * 
         * @throws UnsupportedOperationException if the <tt>put</tt> operation is
         *           not supported by this map.
         * @throws ClassCastException if the class of the specified key or value
         *            prevents it from being stored in this map.
         * @throws IllegalArgumentException if some aspect of this key or value
         *           prevents it from being stored in this map.
         * @throws NullPointerException if this map does not permit <tt>null</tt>
         *            keys or values, and the specified key or value is
         *            <tt>null</tt>.
         */
        V put(K key, V value);
    这个是返回的是一个泛型的Object
    你这么写
    Map map1=(Map<String, Object>) ActionContext.getContext().getSession().put("user", getUsername());
    返回的是put返回的object是null也不为奇怪啊。
    速速结贴速速结贴
      

  16.   

    其中的“user”是如何调用的?请详细说明!