我用的是 struts2+hibernate+spring框架 
想问下 在action中如何传入session?
//rand是jsp页面传过来的验证码
//session.setAttribute("rand",sRand);
下面是我的做法哪里出问题了怎么取不到验证码?
private HttpSession session;
String rand = (String)session.getAttribute("rand"); 请大家帮帮忙谢谢了 急用..

解决方案 »

  1.   

    建议楼主去搜索引擎查一下,struts2如何使用servlet的api,如何使用session\request\response\application等元素。答案很好找,我就不贴代码了。private HttpSession session; 
    String rand = (String)session.getAttribute("rand"); 
    这个如果用在没有继承和实现xxx类xxx接口的action中时肯定要出错的,系统不会自动给你的session变量进行有意义的初始化,就算给出了他的setter方法也是如此。
      

  2.   

    ServletActionContext.getRequest().getSession();
      

  3.   

    jsp中的东西干嘛要放到session中?要在action中取 当个参数传过来不就ok
      

  4.   

    我有用到
    extends ActionSupport implements ServletRequestAware, ServletResponseAware
    不是报错问题,而是取不到值
      

  5.   

    request.getSession().getAttribute()/setAttribute
      

  6.   

    session都是空的,怎么可能不报错呢.正解
      

  7.   

    那还是照2楼说的写吧。ServletActionContext.getRequest().getSession(); 
      

  8.   

    谢谢大家 不过我是照5楼说的 弄出来了 
    String rand=(String)request.getSession().getAttribute("rand");