Struts2 提供了一个将对象放入HttpSession的方法. 使用SessionAware
package org.apache.struts2.interceptor;
import java.util.Map;
public interface SessionAware {
public void setSession(Map<String,Object> session);
}
当某个类需要向HTTPSession中写对象的时候, 需要实现该接口,得到一个Map对象的引用, 将需要放入HttpSession中的对象放入Map对象中
public class HelloWorldAction extends ActionSupport implements SessionAware, ParameterNameAware {
private void increaseHelloCount() {
Integer helloCount = (Integer) userSession.get(HELLO_COUNT);
if (helloCount == null ) {
helloCount = 1;
} else {
helloCount++;
}
userSession.put(HELLO_COUNT, helloCount);
}
public void setSession(Map<String, Object> session) {
userSession = session ;
}
}