请问在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);
[解决办法]
HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); session.setAttribute("username", 123456);
[解决办法]