由于您没有发布任何代码,我不确定您使用的验证方法是什么,或者您尝试了什么。以下是使用validate()
方法处理表单验证的示例操作。
我原本忘记提及您需要更改表单,以便提交给“myAction!submit”而不仅仅是“myAction”。
public class MyAction extends ActionSupport {
/**
* Your profile, as loaded from the database.
*/
private Profile profile;
/**
* SkipValidation is required so that the validate() method is not invoked
* prior to the execute() method. Otherwise, all fields would be blank and
* we would end up showing validation errors on all fields.
*/
@SkipValidation
@Override
public String execute() throws Exception {
return INPUT;
}
@Override
public void validate() {
if (StringUtils.isEmpty(profile.getUsername())) {
addFieldError("profile.username", "User Name is required.");
}
// place any additional validations here...
// you can and should create convenience methods for performing validations.
}
public String submit() throws Exception {
// persist the changes and return an appropriate response
// e.g., redirect somewhere
return SUCCESS;
}
}
显示动作和jsp。 – Quaternion 2011-03-07 14:58:26
您使用的是什么Struts2验证方法? 'validate()'方法或XML驱动验证? – 2011-03-07 16:32:46