背景:我将Struts2与REST和约定插件一起使用,因此大约99%的设置都在我编写的类中,而不是在xml文件中。使用 Convention,您可以使用方法上的注释配置服务器和客户端验证,例如,我使用的帐户创建方法如下所示:
@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()
方法被正确调用并且一切正常。
问题在于绕过客户端验证并在服务器端验证失败时。所有文档都告诉我,验证拦截器将用户发送回设置了正确字段错误的表单,以便用户可以解决他们的问题,但在我的应用程序中,它只是重定向到一个完全空白的页面。
问题 - 如何告诉验证拦截器表单要重定向到哪里,以便可以填写值并正确设置字段错误?
不知道您的配置,很难说出了什么问题。
我的猜测是您只为您的操作配置了验证拦截器例:
// this is wrong
<action name="doSomething" class="DoSomethingAction">
<interceptor-ref name="validation">
</interceptor-ref>
</action>
这里只调用验证拦截器,而不调用其他拦截器,如工作流
// this is better
<action name="doSomething" class="DoSomethingAction">
<interceptor-ref name="defaultStack">
</interceptor-ref>
</action>