日期:2014-05-19  浏览次数:20614 次

struts2的ActionContext.getContext().getSession()
我在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方面的功能,
我这样理解有错吗?

------解决方案--------------------
还没结啊???
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也不为奇怪啊。
速速结贴速速结贴