public class BaseAction extends ActionSupport {
protected PageBean pageBean = new PageBean();
//获取分页实体类
public PageBean getPageBean(){
return this.pageBean;
}
public void setPageBean(PageBean pageBean){
this.pageBean = pageBean;
}
//获取请求对象
public HttpServletRequest getHttpServletRequest(){
return (HttpServletRequest)ActionContext.getContext().get("com.opensymphony.xwork2.dispather.HttpServletRequest");
}
//获取响应对象
public HttpServletResponse getHttpServletResponse(){
return (HttpServletResponse)ActionContext.getContext().get("com.opensymphony.xwork2.dispather.HttpServletResponse");
}
//获取session
public Map getSession(){
return ActionContext.getContext().getSession();
}
//jsp内置对象获取session
public HttpSession getHttpSession(){
return ServletActionContext.getRequest().getSession();
}
//获取会话保存的信息
public Admin getServerSession(){
return (Admin)getHttpSession().getAttribute("admin");
}
}
Struts2 中action获取session也有另一种方法:
Action实现org.apache.struts2.interceptor.SessionAware(获取request用org.apache.struts2.interceptor.RequestAware)接口的方式来对session进行操作,加入如下代码:
Map<String,Object> session;
@Override
public void setSession(Map<String,Object> session){
this.session=session;
}
获取request则如下:
Map<String,Object> request;
@Override
public void setRequest(Map<String,Object> request){
this.request=request;
}
将以上内容放到BaseAction里作用一样,