我在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 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<String, Object>) ActionContext.getContext().getSession();
map1.put("user",getUsername());
你这样:
Map map1=(Map<String, Object>) ActionContext.getContext().getSession();
map1.put("user", getUsername());
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)
怎样判断 class org.apache.struts2.dispatcher.SessionMap是map的子类?
而却Boolean o9=ActionContext.getContext().getSession().getClass().isInstance(Map.class)
o9是false,
怎么办
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是个接口
文档里面是这样描述put的返回的:
返回:
以前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null。(如果该实现支持 null 值,则返回 null 也可能表示此映射以前将 null 与 key 关联)。 所以返回的是value,不是你的map。除非它本身value里存的是map。
楼主玩脱了,先看看基础滴把
你想要往session里赋值
7楼:
struts2么,建议用ServletActionContext.getRequest().getSession()
你的这个写法错的是基础
3楼:
你最后调用的是put方法,其实Map map1接受的是put的返回值,而不是getsession的返回值,put方法的返回值是void我记得。所以导致了你的map1是null
楼主加油。
这句代码能编译过???
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也不为奇怪啊。
速速结贴速速结贴