日期:2014-05-18  浏览次数:20753 次

struts2 通过ioc获取session为空
最近在学struts2,定义了一个BaseAction通过IoC方式实现SessionAware接口获取session,其他类想用session的时候继承BaseAction,但是出现session为空,不知道为什么?代码如下
BaseAction类:
package common;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author qgs Class Description:用户Action类 Created Time:2014-3-17 下午02:45:35
 */
public class BaseAction extends ActionSupport implements SessionAware,RequestAware {

 /* 
  获取request,response,session方式一,非IoC方式,不用实现SessionAware,
 ServletRequestAware, ServletResponseAware
 ActionContext context = ActionContext.getContext();
 HttpServletRequest request = (HttpServletRequest)
 context.get(ServletActionContext.HTTP_REQUEST);
 HttpServletResponse response = (HttpServletResponse)
 context.get(ServletActionContext.HTTP_RESPONSE);
 Map session = context.getSession();
 SessionMap session = (SessionMap) context.get(ActionContext.SESSION);

  获取request,response,session方式一,IoC方式,必须实现SessionAware,
 RequestAware或者ServletRequestAware, ServletResponseAware
 */

private static final long serialVersionUID = 1L;

private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;

public Map<String, Object> getApplication() {
return application;
}

public Map<String, Object> getRequest() {
return request;
}
public Map<String, Object> getSession() {
return session;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}


}

UserAction类:
package kmust.liip.testcenter.userright.action;

import java.util.ArrayList;
import java.util.List;

import kmust.liip.testcenter.userright.dao.BaseDao;
import kmust.liip.testcenter.userright.dao.RightSeqDaoImpl;
import kmust.liip.testcenter.userright.dao.UserDao;
import kmust.liip.testcenter.userright.dao.UserDaoImpl;
import kmust.liip.testcenter.userright.model.User;

import org.hibernate.HibernateException;

import common.BaseAction;

/**
 * @author qgs
 * Class Description:用户Action类
 * Created Time:2014-3-17 下午02:45:35
 */
public class UserAction extends BaseAction {

private static final long serialVersionUID = 1L;

private User user;

//用户登录
public String login() {
String usercode = this.user.getUsercode();
String