是否可以从struts2中的Action类调用validate方法?
所以基本上我们想要在动作类中控制之后在struts 1中验证字段我们可以通过以下方式实现:
@Override
public ActionForward saveUser(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws IOException {
UserForm form = (UserForm ) actionForm;
ActionErrors errors = actionForm.validate(mapping, request);
if (!errors.get("password").hasNext() && form.getPassword() != null
...
...
...
答案 0 :(得分: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;
}
}
还有一个很好的教程https://dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-validation-example-1.html