2

背景:我将 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?

4

1 回答 1

0

在不知道您的配置的情况下,很难判断出了什么问题。

我的猜测是您只为您的操作配置了验证拦截器示例:

// 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>
于 2013-02-20T10:26:46.403 回答