这几天写一个网站项目
在action的参数定义中用ActionContext.getContext().getSession()出错
但把ActionContext.getContext().getSession()放到函数中就不出错了啊
不知道是什么原因,求高手解答
public class SalerOrderAction extends BaseOdListAction 
{
private SellServiceImpl sellService=new SellServiceImpl();
Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");//@@@@出错代码 public String showAllOd()
{
String restr="allOd";
Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");//@@@@把出错代码写到这就没错了啊
System.out.println("user.getUname()"+user.getUname());
try
{
this.odListSum=sellService.getMyOdAllSum();
this.pageSum=(int)(Math.ceil((double)this.odListSum/20));
if(currentPage<=0) currentPage=1;
if(currentPage>this.pageSum) currentPage--;
this.odList=sellService.getMyOdAll(this.currentPage);
this.pageType="allOd";
}
catch(Exception e)
{
this.addActionError(e.getMessage());
}
return restr;
}
}

解决方案 »

  1.   

    是因为,之前put进session里面的数据还没真正的写进去???
      

  2.   

    还是不行啊
    我加了一个延迟处理的Thread.sleep(5000),还是报错啊
    public class SalerOrderAction extends BaseOdListAction 
    {
    private SellServiceImpl sellService=new SellServiceImpl();
    //Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");
    static 
    {
    try{
    Thread.sleep(5000);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    System.out.println("静态代码块,已执行!");
    Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");
    System.out.println(user.getUname());
    }
    public String showAllOd()
    {
    String restr="allOd";
    Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");
    System.out.println("user.getUname()"+user.getUname());
    try
    {
    this.odListSum=sellService.getMyOdAllSum();
    this.pageSum=(int)(Math.ceil((double)this.odListSum/20));
    if(currentPage<=0) currentPage=1;
    if(currentPage>this.pageSum) currentPage--;
    this.odList=sellService.getMyOdAll(this.currentPage);
    this.pageType="allOd";
    }
    catch(Exception e)
    {
    this.addActionError(e.getMessage());
    }
    return restr;
    }
      

  3.   

    写在方法外,则是在构造方法之前执行。
    写在方法中,是在构造方法之后执行。
    由此很容易得到,ActionContext的赋值在构造方法中。
      

  4.   

    在SalerOrderAction 构造器中初始化。
    我怀疑现在的ActionContext.getContext()得到的就是空值
      

  5.   

    你可以重写构造方法,在构造方法中,先super(),再做你要做的那个赋值。
      

  6.   

    ActionContext.getContext()不是空的啊我把它分解了
    Map session=ActionContext.getContext().getSession();
    Tuser user=(Tuser)session.get("user");//这一句出错的啊
      

  7.   

    我在向session.put的地方就可以获取user对象
    而且也可以肯定是先put进去了
    我再get的啊
                            Map<String,Tuser> session=ActionContext.getContext().getSession();
    session.put("user", user);

    System.out.println("has put user.name"+session.get("user").getUname());
    Map<String,Tuser> session2=ActionContext.getContext().getSession();

    System.out.println("uid========"+session2.get("user").getUid());
      

  8.   

    呵呵
    突然发现确实ActionContext.getContext()得到的就是空值
      

  9.   

    struts2的文档中写的很清楚,不要在构造函数中使用ActionContext.getContext().
      

  10.   

    http://blog.csdn.net/alex197963/archive/2008/03/26/2219912.aspx
    说的很好 俺学的时候都没有这么好的文章 在使用ActionContext时有一点要注意:不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null.太具体的俺也不太清楚了 毕竟俺不用struts2
      

  11.   

    另外 你那个什么Thread.sleep该让我咋形容好呢。。
      

  12.   

    每次执行Action之前都会创建新的ActionContext,
    使用ActionContext时要注意:不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null。
      

  13.   

       在使用ActionContext时有一点要注意: 不要在Action的构造函数里使用ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过ActionContext取得的值也许是null;Tuser user=(Tuser)ActionContext.getContext().getSession().get("user");这句话不要放在构造函数中,也不要直接放在类中,而应该放在每个具体的方法体中(eg:login()、queryAll()、insert()等),这样才能保证每次产生对象时独立的建立了一个user。
      

  14.   

    我把它写到构造方法里面了,可还是session 为空啊
    public SalerOrderAction()
    {
    super();
    System.out.println("构造方法,已执行!");
    Map session=ActionContext.getContext().getSession();
    System.out.println("session="+session);
    Tuser user=(Tuser)session.get("user");
    }
      

  15.   

    要放在struts2的生命周期里面。
    可参考struts2的执行过程