日期:2014-05-17  浏览次数:20703 次

Struts2生成验证码放入session,action无法得到session
在一个RandomAction中生成一个验证码后通过
 ActionContext.getContext().getSession().put("random", rdnu.getString());
将生成的验证码字符串保存到session里,供用户登录或注册时验证是否输入正确
在RandomAction中使用
ActionContext.getContext().getSession().get("random"),可以正常获得session中值
但在另一个LoginAction中,同样的方法,答应出来的确是null;
请问如何解决。
代码如下:
RandomAction.java
Java code

import java.io.ByteArrayInputStream;
import Web.RandomNumUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RandomAction extends ActionSupport{   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ByteArrayInputStream inputStream;   
    public String execute() throws Exception{   
        RandomNumUtil rdnu=RandomNumUtil.Instance();   
        this.setInputStream(rdnu.getImage());
        ActionContext.getContext().getSession().put("random", rdnu.getString());
        System.out.println("生成验证码:"+rdnu.getString());
        System.out.println("获得验证码:"+ActionContext.getContext().getSession().get("random"));
        return SUCCESS;   
    }   
    public void setInputStream(ByteArrayInputStream inputStream) {   
        this.inputStream = inputStream;   
    }   
    public ByteArrayInputStream getInputStream() {   
        return inputStream;   
    }
}


RandomNumUtil.java(验证码生成方法类)
Java code

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

public class RandomNumUtil {   
    privateprovider="java:MSG">


LoginAction.java
Java code
import com.ccniit.service.UserManager; 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport  {

/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private String username;
private String password1;
private String vercode;

public String getVercode() {
return vercode;
}



public void setVercode(String vercode) {
this.vercode = vercode;
}

private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

protected UserManager mgr;


public void setMgr(UserManager mgr) {
this.mgr = mgr;
}


public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword1() {
return password1;
}

public void setPassword1(String password1) {
this.password1 = password1;
}
public String showReg() throws Exception {
return SUCCESS;
}
public String login() throws Exception {
String ver2=(String) ActionContext.getContext().getSession().get("random");
System.out.println("random:" + ver2);
System.out.println("用户输入:" + getVercode());
if (getVercode().equals(getVercode())) {
if (mgr.login(getUsername(), getPassword1()) > 0)