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

swoky 2010-06-30 08:58:15
目前项目中用到struts2,取session是通过实现SessionAware接口来取的,但是这时struts2返回的是一个Map,我现在想取到其中的sessionid,该怎么取???????
同时又不想用request.getSession().getId()的方式(我知道这样是可以取到),因为之前的程序己写完,我不想改己经写好的东西,
哪位牛人知道告知下(baidu,google,均未果),先100分送上,如不够,再加
...全文
376 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)
压缩文件 收集的常见的专业问题解决办法.rar 2006-11-01 21:10 94792 91186 常见的专业问题解决办法\CSS语法一览表.pdf 2009-03-05 14:06 155509 34856 常见的专业问题解决办法\html中rel与rev的关系(转载)_我的幅度.mht 2009-03-05 19:33 232863 45314 常见的专业问题解决办法\Html标签大全_破巢.mht 2006-11-01 21:11 107135 101736 常见的专业问题解决办法\HTML语法一览表.pdf 2000-12-29 09:23 53412 5232 常见的专业问题解决办法\HTML语言参考\HTM1.HTM 2000-12-29 09:23 22961 3263 常见的专业问题解决办法\HTML语言参考\HTM2.HTM 2009-01-16 17:08 1494866 1050112 常见的专业问题解决办法\JavaScript特效代码集.rar 2006-11-01 21:11 95888 88773 常见的专业问题解决办法\JavaScript语法一览表.pdf 2009-02-24 08:39 1423088 975149 常见的专业问题解决办法\Java中多态变量的讨论和总结.mht 2009-02-24 08:52 156256 34316 常见的专业问题解决办法\Java中的强制类型转换_Believe ┭┮ YourSelf.mht 2009-02-24 08:31 61003 61003 常见的专业问题解决办法\Java容器类List、ArrayList、Vector及map、HashTable、HashMap的使用与区别.rar 2009-02-24 08:29 40960 13763 常见的专业问题解决办法\java容器类介绍.doc 2009-02-24 08:42 165165 37065 常见的专业问题解决办法\Java核心技术学习笔记--异常和调试_Believe ┭┮ YourSelf.mht 2009-03-20 16:36 142683 39110 常见的专业问题解决办法\JR - 专题论坛问题 - eclipse??如何设置代理啊.mht 2009-03-10 17:37 462569 194709 常见的专业问题解决办法\JSP实例:动态生成验证码.mht 2009-03-10 17:52 185677 39758 常见的专业问题解决办法\jsp数字验证码代码详解 - 我爱跑IT---技术专区.mht 2009-03-10 17:22 192548 40484 常见的专业问题解决办法\JSP验证码代码的实现_娟行天下.mht 2009-02-24 08:40 156913 33699 常见的专业问题解决办法\JS对select控件的操作汇总整理.mht 2009-02-24 08:40 173330 36602 常见的专业问题解决办法\Js获取当前日期时间及其它操作_Believe ┭┮ YourSelf.mht 2009-03-09 09:42 159485 36202 常见的专业问题解决办法\MYSQL命令备忘_心心相惜.mht 2009-02-24 09:01 165033 37357 常见的专业问题解决办法\MySql的配置_Believe ┭┮ YourSelf.mht 2009-02-24 08:43 452690 186879 常见的专业问题解决办法\Rational Rose 2003 下载、破解及安装方法(图文)_Believe ┭┮ YourSelf.mht 2009-02-24 08:36 171617 47585 常见的专业问题解决办法\Spring框架与AOP思想的研究与应用.mht 2009-02-24 08:37 190012 61182 常见的专业问题解决办法\Spring框架快速入门之简介.mht 2009-01-16 08:16 281434 266693 常见的专业问题解决办法\sql导入excel.rar 2009-03-10 11:12 181976 38906 常见的专业问题解决办法\struts的验证码实现_17号的空间.mht 2009-03-10 11:13 377530 94291 常见的专业问题解决办法\struts验证码 - Struts - Java - JavaEye论坛.mht 2007-10-20 11:27 427008 296205 常见的专业问题解决办法\swing程序设计.doc 2007-12-25 20:21 1504314 1493642 常见的专业问题解决办法\Tomcat.chm 2009-02-21 15:33 979 696 常见的专业问题解决办法\U盘新玩法.txt 2009-03-16 17:34 93262 24870 常见的专业问题解决办法\[]hibernate中的Session什么时候关闭?_百度知道.mht 2009-03-18 09:07 2303315 1322088 常见的专业问题解决办法\[教程]红旗Linux5_0桌面正式版光盘安装=图解教程=Unix-Linux - 系统之家论坛.mht 2009-02-24 08:43 151144 33570 常见的专业问题解决办法\“在静态方法中不能调用非静态的方法和引用非静态的成员变量”如何理解?_Believe ┭┮ YourSelf.mht 2009-03-03 20:16 487640 194474 常见的专业问题解决办法\代理知识大全~~~【申精】精品软件 - 玄殿社区 —打造游戏娱乐温馨家园.mht 2009-03-17 12:00 166432 35921 常见的专业问题解决办法\最新VM虚拟机6_0版_可可_KEY.mht 2009-03-19 08:29 99829 27128 常见的专业问题解决办法\求网页设计,屏蔽右键的代码?_百度知道.mht 2009-03-09 09:38 132723 34712 常见的专业问题解决办法\百度_网盾工程吧_MySQL导出导入命令.mht 2003-06-20 20:49 153581 146314 常见的专业问题解决办法\精通swing程序设计.chm 2009-02-26 16:33 177152 37861 常见的专业问题解决办法\网页常用的jsp 脚本.doc 2009-03-20 16:37 254350 86752 常见的专业问题解决办法\解决Struts中ApplicationResources_properties文件不支持中文问题_微科工作室 QQ:28790953____.mht 2009-02-25 08:45 文件夹 文件夹 常见的专业问题解决办法\HTML语言参考 2009-03-21 14:55 文件夹 文件夹 常见的专业问题解决办法 # # 总计 大小 压缩后大小 文件数 # 13349124 7429458 42

81,116

社区成员

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

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