Validation 我们可以从struts2中的action类调用form validate方法吗?

Validation 我们可以从struts2中的action类调用form validate方法吗?,validation,struts2,action,Validation,Struts2,Action,可以从struts2中的Action类调用validate方法吗? 因此,基本上,我们希望在验证了struts 1中的字段后,在action类中获得控制权,我们可以通过以下方式实现: 您可以使用以下内容: public class Login extends ActionSupport { private String userName; private String password; public Login() { } public String execute() { r

可以从struts2中的Action类调用validate方法吗? 因此,基本上,我们希望在验证了struts 1中的字段后,在action类中获得控制权,我们可以通过以下方式实现:


您可以使用以下内容:

public class Login extends ActionSupport {

private String userName;
private String password;

public Login() {
}

public String execute() {
    return SUCCESS;
}

public void validate() {
    if (getUserName().length() == 0) {
        addFieldError("userName", "User Name is required");
    } else if (!getUserName().equals("Eswar")) {
        addFieldError("userName", "Invalid User");
    }
    if (getPassword().length() == 0) {
        addFieldError("password", getText("password.required"));
    }
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

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

还有一个很好的教程

为什么不呢?你试过了吗?为什么要试?用例是什么?用例如下:在验证之前,我们需要做一些预处理,比如在会话中设置一些属性等。然后需要执行验证,但我们也需要做一些验证后处理。因此需要从操作本身调用validate方法。有什么建议吗?如果有验证,就不需要了堆栈上的拦截器。它执行验证,工作流返回无效结果,因此该操作永远不会执行。您好,我有兴趣多次执行相同的验证方法(我用case控制哪个部分将被计算),但这只是第一次工作,对于我来说,如果您有一个完整的Struts 1问题示例,我将非常感谢您。谢谢
public class Login extends ActionSupport {

private String userName;
private String password;

public Login() {
}

public String execute() {
    return SUCCESS;
}

public void validate() {
    if (getUserName().length() == 0) {
        addFieldError("userName", "User Name is required");
    } else if (!getUserName().equals("Eswar")) {
        addFieldError("userName", "Invalid User");
    }
    if (getPassword().length() == 0) {
        addFieldError("password", getText("password.required"));
    }
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

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