struts2 的Action中若希望访问Session对象,可采用两种方式:
1、从ActionContext中获取;
2、实现SessionAware接口。
1、从ActionContext中获取
Map<String , Object > session = ActionContext.getContext().getSession(); session.put("userName", user.getUserName());
2、实现SessionAware接口:
...... public class Login extends ActionSupport implements SessionAware{ Map<String , Object > session;
....... @Override public String execute() throws Exception { Map<String , Object > session = ActionContext.getContext().getSession(); session.put("userName", "value"); return "success"; }
@Override public void setSession(Map<String, Object> arg0) { this.session = arg0; }
}