首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

请教在struts2里如何用session

2012-01-03 
请问在struts2里怎么用session?class testAction{public void test1() {ActionContext.getContext().getSe

请问在struts2里怎么用session?
class testAction{
  public void test1() {
  ActionContext.getContext().getSession().put("username", "123456");
  System.out.println(ActionContext.getContext().getSession().get("username"));
  }
  public void test2() {
  System.out.println(ActionContext.getContext().getSession().get("username"));
  }
}

当我访问 test!test1.do 的时候,打印了 123456 当我访问 test!test2.do 的时候打印null
浏览器的进程没改变过!
session里保存不了数据。。请问我现在想要做一个登陆程序,该怎么做啊?

[解决办法]
//获取会话HttpSession,被抽象成Map接口
Map map = ActionContext.getContext().getSession();
map.put("name", name);
[解决办法]

Java code
        HttpServletRequest request = ServletActionContext.getRequest();        HttpSession session = request.getSession();                session.setAttribute("username", 123456);
[解决办法]
探讨
谢谢楼上2位,这2个方法我都试过啦。
在index!test1.do 设置了session 后
到index!test2.do 打印出来为空 为什么?

[解决办法]
http://hi.baidu.com/83951150/blog/item/3e1d30ec2c59a52263d09f77.html
[解决办法]
ServlectActionContext.getRequest().getSession();

ActionContext.getContext().getSession(); 

热点排行