日期:2014-05-20  浏览次数:20864 次

struts 中用addFieldError("noTypeCode", "错误!"); 二次提交却不调用action,注释之后action却可以调用。
调用方法添加报错信息: 
public void savePubBroker(DealerPubBroker dealerPubBroker) { 
Integer brokerId=dealerPubBroker.getBrokerId(); 
//brokerId为空代表是新增 否则为修改 
if(brokerId==null){ 
ResultInfo<DealerPubBroker> resultInfo=pubBrokerDAO.findBrokerByCpid(dealerPubBroker.getCpId()); 
if(resultInfo.getResultList().size()==0){ 
if(dealerPubBroker.getLoginMode()==null){ 
throw new AppException("请选择至少一种客户标识类型!"); 
}else{ 
pubBrokerDAO.save(dealerPubBroker); 

}else{ 
throw new AppException("该券商代码已使用,请重新定义!"); 

}else{ 
try{ 
pubBrokerDAO.save(dealerPubBroker); 
}catch(Exception e){ 
throw new AppException("操作失败!"); 





action 中添加错误信息: 
try{ 
pubBrokerServ.savePubBroker(dealerPubBroker); 
}catch(AppException ae){ 
if(typeCode==null&&loginMode==null){ 
addFieldError("noTypeCode", ae.getMessage()); 
}else{ 
addFieldError("isUsed", ae.getMessage()); 

dois="add"; 
typeCode=null; 
return "showAddBroker"; 



得到报错页面后: 


再次提交后却不调用action了 
当把action中的addFieldError("noTypeCode", ae.getMessage());注释掉 之后却可以二次提交,也调用了后台action 
try{}catch(Exception e){}不到任何信息。这是为什么?我觉得是addFieldError 的问题。

希望能有好心人帮忙指点,问题出在哪,在此感激各位!! 


------解决方案--------------------
验证的方法在action里应该放在 validate 方法下:

如:
Java code
public class LoginAction extends ActionSupport {

    private String name;
    private String password;
    private String rePassword;
    private String age;
    private Date date;
    private String email;

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void validate() {
        if (name.equals("") || name == null) {
            this.addFieldError("name", "姓名不能为空!");
        } else if (!name.matches("[[a-z]|[A-Z]]*")) {
            this.addFieldError("name", "姓名只能字母!");
        }
        if (!age.matches("\\d+")) {
            this.addFieldError("age", "年龄必须为数字!");
        } else if (Integer.valueOf(age) < 1 || Integer.valueOf(age) > 150) {
            this.addFieldError("age", "年龄不符合规范!");
        }
        if (password.matches("") || password == null) {
            this.addFieldError("password", "密码不能为空!");
        } else if (!rePassword.matches(password)) {
            this.addFieldError("rePassword", "两次输入的密码不同!");
        }
        if (!email
                .matches("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\\w]*[0-9a-zA-Z])*\\.)+[a-zA-Z]{2,9})$")) {
            this.addFieldError("email", "邮箱必须符合规范!");
        }

    }

    public String execute() {

        if (name.equals("123") && password.equals("123")
                && rePassword.equals(password)) {
            return SUCCESS;

        } else {
            return INPUT;
        }

    }

    public String getRePassword() {
        return rePassword;
    }

    public void setRePassword(String rePassword) {
        this.rePassword = rePassword;
    }
}