解决方案 »

  1.   

    要看你的值放在哪个级别,一般struts的都是在request级别,所以第二个不可能有第一个acton的东西,除非你request.getSession().setXXX();
      

  2.   

    session 所有的action都可以去,又能你放进去的就是null
      

  3.   

    首先保证存放到session中没问题,然后看看Action中是否有销毁session中的键值。。调试看看不就知道了。。
      

  4.   

    public class ActionBase extends ActionSupport implements SessionAware,
    RequestAware { protected Map<String, Object> session;
    protected Map<String, Object> request;
    public void setSession(Map<String, Object> session) {
    this.session = session;
    } public void setRequest(Map<String, Object> request) {
    this.request = request;
    }}
    楼上的大家根据我的实际情况给我个从第二个Action中取第一个Action中存入session的值的代码谢谢了
      

  5.   

    楼上的你是说我没有定义getsession方法吗?
      

  6.   

    第一个action:
    package org.fly.netctoss.action;import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
    import org.apache.struts2.ServletActionContext;
    import org.fly.netctoss.util.ImageUtil;import com.opensymphony.xwork2.ActionSupport;
    public class ImageAction extends ActionSupport {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //input 无
    private InputStream imageStream; //output,利用stream输出 public String execute(){
    //创建一个验证码图片
    Map<String,BufferedImage>map=
    ImageUtil.createImage();
    //获取图片字符,写入session
    String code=map.keySet().iterator().next();
    System.out.println(code);
    //ActionContext.getContext().getSession().put("image_code", code);
    //session.put("image_code", code);
    ServletActionContext.getContext().getSession().put("image_code", code);

    //session.setAttribute("image_code", code);
    // ActionContext.getContext().getSession().put("image_code",code);
    //将图片信息给imageStream赋值
    //System.out.println(session.get("image_code"));
    //BufferedImage image=map.get(code);
    //String code1=(java.lang.String) ActionContext.getContext().getSession().get("image_code");
    BufferedImage image=map.get(code);
    try{
    imageStream=
    ImageUtil.getInputStream(image);
    return "success";//调用stream result输出
    }catch(IOException e){
    e.printStackTrace();
    return "error";//定位到错误页面响应
    }
    }
    public InputStream getImageStream(){
    return imageStream;
    }
    public void setImageStream(InputStream imageStream) {
    this.imageStream = imageStream;
    }
    }
      

  7.   

    第二个Action:
    package org.fly.netctoss.action;import java.util.Map;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    public class validImageAction extends ActionSupport  {
    //Struts2中Map类型的session

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //input
    private String image;
    //output  以json格式输出
    private boolean ok = false;//false 重复,true可用

      
    public String execute(){
    //System.out.println(session.get("image_code"));
    try {
    System.out.println(image  );
    //String code1=(String) session.getAttribute("image_code");
    //String code1=  (String) session.values().iterator().next();
    //System.out.println(session.values().iterator().next());
            //String code1=(String)(ActionContext.getContext().getSession().get("image_code"));//取得session保存中的字符串     
                 // String code1=(String)request.get("image_code");
    //String code1= (String) session.get("image_code");
    String str=(String)ActionContext.getContext().getSession().get("image_code");   
    System.out.println(str);  
    //String code=session.get("image_code").toString();
    //System.out.println(code1);

    if(image!=null&&str.equals(image)){//根据cost状态设置ok值
    ok = false;
    }else{
    ok = true;
    }
    return "success";//以json方式输出ok
    } catch (Exception e) {
    e.printStackTrace();
    return "error";
    }
    } public String getImage() {
    return image;
    } public void setImage(String image) {
    this.image = image;
    } public boolean isOk() {
    return ok;
    } public void setOk(boolean ok) {
    this.ok = ok;
    }public static void main(String[] args) {
        String code1=(String)(ActionContext.getContext().getSession().get("image_code"));//取得session保存中的字符串     
    System.out.println(code1);
    }

    }
      

  8.   

     validImageAction extends ActionSupport  >>>>>> extends ActionBase
      

  9.   

    楼上的,我之前就是 extends ActionBase 这样做的也取不到啊
      

  10.   

    我找到原因了,也分享一下给新手,希望他们在编程之路上少走弯路,原因是tomcat不支持验证码ajax验证的原因,换jboss就可以了。