【问题标题】:Struts2 validation interceptor going to xhtml documentStruts2 验证拦截器转到 xhtml 文档
【发布时间】:2012-06-18 18:02:52
【问题描述】:

背景:我正在使用带有 REST 和 Convention 插件的 Struts2,所以我大约 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?

【问题讨论】:

    标签: validation xhtml struts2 action interceptor


    【解决方案1】:

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

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

    // 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多