背景:我将 Struts2 与 REST 和 Convention 插件一起使用,因此大约 99% 的设置在我编写的类中,而不是在 xml 文件中。使用约定,您可以使用方法上的注释配置服务器和客户端验证,例如,我使用的帐户创建方法如下所示:
@Validations(
requiredFields = {
@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "userName", message = "You must enter a value for field."),
@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "firstName", message = "You must enter a value for field."),
@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "lastName", message = "You must enter a value for field."),
@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "password", message = "You must enter a value for field.")
},
emails = {@EmailValidator(type = ValidatorType.SIMPLE, fieldName = "email", message = "You must enter a value for email.")},
stringLengthFields = {
@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength = "6", maxLength = "16", fieldName = "userName", message = "Username must be at least 6 letters."),
@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength = "8", maxLength = "16", fieldName = "password", message = "Password must be at least 8 characters.")
}
)
public String create() {
//create the account
}
这很好用,javascript 被正确推送到 JSP,表单在提交之前经过验证,服务器端验证也很好用,如果满足所有条件,create()
则正确调用方法并且一切正常。
问题在于绕过客户端验证并在服务器端验证失败。所有文档都告诉我,验证拦截器将用户发送回表单并设置了正确的字段错误,以便用户可以解决他们的问题,但在我的应用程序中,它只是重定向到一个完全空白的页面。
问题 - 如何告诉验证拦截器将表单重定向到哪里,以便可以填写值并正确设置 fieldErrors?