struts2 session问题,急!!!!!!!!!!!!!!

swoky 2010-06-30 08:58:15
目前项目中用到struts2,取session是通过实现SessionAware接口来取的,但是这时struts2返回的是一个Map,我现在想取到其中的sessionid,该怎么取???????
同时又不想用request.getSession().getId()的方式(我知道这样是可以取到),因为之前的程序己写完,我不想改己经写好的东西,
哪位牛人知道告知下(baidu,google,均未果),先100分送上,如不够,再加
...全文
341 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sugarqian 2011-09-19
  • 打赏
  • 举报
回复
ServletActionContext.getRequest().getSession().getId();可以取到sessionid。
wen5200a 2010-07-01
  • 打赏
  • 举报
回复
ActionContext.getContext().getSession().get("key");
struts2中可以这么获得session的。
closewbq 2010-07-01
  • 打赏
  • 举报
回复

public class SessionMap<K, V> extends AbstractMap<K,V> implements Serializable {

private static final long serialVersionUID = 4678843241638046854L;

protected HttpSession session;
protected Set<Map.Entry<K,V>> entries;
protected HttpServletRequest request;


/**
* Creates a new session map given a http servlet request. Note, ths enumeration of request
* attributes will occur when the map entries are asked for.
*
* @param request the http servlet request object.
*/
public SessionMap(HttpServletRequest request) {
// note, holding on to this request and relying on lazy session initalization will not work
// if you are running your action invocation in a background task, such as using the
// "execAndWait" interceptor
this.request = request;
this.session = request.getSession(false);
}

/**
* Invalidate the http session.
*/
public void invalidate() {
if (session == null) {
return;
}

synchronized (session) {
session.invalidate();
session = null;
entries = null;
}
}

/**
* Removes all attributes from the session as well as clears entries in this
* map.
*/
public void clear() {
if (session == null ) {
return;
}

synchronized (session) {
entries = null;
Enumeration<String> attributeNamesEnum = session.getAttributeNames();
while(attributeNamesEnum.hasMoreElements()) {
session.removeAttribute(attributeNamesEnum.nextElement());
}
}

}

/**
* Returns a Set of attributes from the http session.
*
* @return a Set of attributes from the http session.
*/
public Set<java.util.Map.Entry<K,V>> entrySet() {
if (session == null) {
return Collections.emptySet();
}

synchronized (session) {
if (entries == null) {
entries = new HashSet<Map.Entry<K,V>>();

Enumeration<? extends Object> enumeration = session.getAttributeNames();

while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = session.getAttribute(key);
entries.add(new Map.Entry<K,V>() {
public boolean equals(Object obj) {
if (!(obj instanceof Map.Entry)) {
return false;
}
Map.Entry<K, V> entry = (Map.Entry<K, V>) obj;

return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
}

public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}

public K getKey() {
return (K) key;
}

public V getValue() {
return (V) value;
}

public V setValue(Object obj) {
session.setAttribute(key, obj);

return (V) value;
}
});
}
}
}

return entries;
}

/**
* Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
*
* @param key the name of the session attribute.
* @return the session attribute or <tt>null</tt> if it doesn't exist.
*/
public V get(Object key) {
if (session == null) {
return null;
}

synchronized (session) {
return (V) session.getAttribute(key.toString());
}
}

/**
* Saves an attribute in the session.
*
* @param key the name of the session attribute.
* @param value the value to set.
* @return the object that was just set.
*/
public V put(K key, V value) {
synchronized (this) {
if (session == null) {
session = request.getSession(true);
}
}

synchronized (session) {
entries = null;
session.setAttribute(key.toString(), value);

return get(key);
}
}

/**
* Removes the specified session attribute.
*
* @param key the name of the attribute to remove.
* @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
*/
public V remove(Object key) {
if (session == null) {
return null;
}

synchronized (session) {
entries = null;

V value = get(key);
session.removeAttribute(key.toString());

return value;
}
}


/**
* Checks if the specified session attribute with the given key exists.
*
* @param key the name of the session attribute.
* @return <tt>true</tt> if the session attribute exits or <tt>false</tt> if it doesn't exist.
*/
public boolean containsKey(Object key) {
if (session == null) {
return false;
}

synchronized (session) {
return (session.getAttribute(key.toString()) != null);
}
}

根据sessionMap源码我们可以得知只有get方法才能从Httpsession中取出值。
其实get方法也就是session.getAttribute取出的值。
但是HttpSession实现类中,没有通过session.setAttribute方法存放过sessionid。只有getId这个方法,所以通过sessionMap得到sessionId似乎没有这个接口。
izard999 2010-07-01
  • 打赏
  • 举报
回复
ServletActionContext可以拿到你想要的servletAPI
hack_ccsl 2010-07-01
  • 打赏
  • 举报
回复
闪人。
hack_ccsl 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 swoky 的回复:]
目前项目中用到struts2,取session是通过实现SessionAware接口来取的,但是这时struts2返回的是一个Map,我现在想取到其中的sessionid,该怎么取???????
同时又不想用request.getSession().getId()的方式(我知道这样是可以取到),因为之前的程序己写完,我不想改己经写好的东西,
哪位牛人知道告知下(baidu,google,均未果……
[/Quote]

说出你现在是怎么取的,不然怎么知道写出来会不会让你改已经写好的东西,有困难吗?
stl0 2010-07-01
  • 打赏
  • 举报
回复
ActionContext.getContext().getSession()?
小霍夫 2010-07-01
  • 打赏
  • 举报
回复
ActionContext.getContext().getSession().get("key");
ronniegxq 2010-07-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaoye2892 的回复:]

以上三楼 答非所问
[/Quote]
哈哈
2010-07-01
  • 打赏
  • 举报
回复
getSession().getAttribute("sessionid");
colin_pxx 2010-07-01
  • 打赏
  • 举报
回复
getSession().getAttribute("sessionid");
zheng192004 2010-07-01
  • 打赏
  • 举报
回复
我来学习~
hack_ccsl 2010-07-01
  • 打赏
  • 举报
回复
你之前怎么取的。
swoky 2010-07-01
  • 打赏
  • 举报
回复
可能是我没说清楚,我不是想在页面中获取这个值,是在action中
kdflove 2010-07-01
  • 打赏
  • 举报
回复
路过。。。
swoky 2010-07-01
  • 打赏
  • 举报
回复
To hack_ccsl:
之前程序没用到过sessionid,但是现在要用,并且之前用session都是用的Map形式的get(...)来取值,所以才会有现在的问题...

刚看了closewbq 的回复,然后自己再查了下tomcat的StandardSession实现,的确是如此,没法通过sessionMap来得到sessionid
下面是StandardSession中对sessionid和attributes的定义:

protected Map attributes = new ConcurrentHashMap();//通常的session.getAttribute,session.setAttribute的操作都在此

protected String id = null;//这就是sessionID,session.getId()方法返回的就是它


从sessionMap的entrySet方法可以看出,所有的get,set最终都是针对attributes做的操作,而StandardSession中其它的属性,如id,isNew,maxInactiveInterval.....等等都是没法通过get取到的

谢谢大家的回复!
Ade子夜 2010-06-30
  • 打赏
  • 举报
回复
顶!!!!!!!
happyfmy 2010-06-30
  • 打赏
  • 举报
回复
以上三楼 答非所问
wenlonghor616 2010-06-30
  • 打赏
  • 举报
回复
嗯,用ognl把值从valueStack中拿出来
yonghenghxq 2010-06-30
  • 打赏
  • 举报
回复
嗯,1楼的方法,应该可以。。
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧