日期:2014-05-20  浏览次数:20577 次

请问在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();