validation - 支柱 2 : Apply different XML validation files to one Action Methods

标签 validation struts2 struts-validation

我是 Struts 2 Framework 的新手,我使用 XML 验证文件来验证表单的字段。我的问题是:

  • 如何将不同的 XML 验证文件应用于一个操作的方法?
  • 我还想对多个操作方法使用相同的 XML 验证文件。
  • 最佳答案

    • Q: How can I Apply different XML validation files to the Methods of one Action ?


    您可以使用 Action Alias 命名约定(而不是默认的 Action Class 命名约定)为 Action 的每个方法创建一个 XML 验证文件。

    来自 the docs :

    Defining Validation Rules

    Validation rules can be specified:

    1. Per Action class: in a file named ActionName-validation.xml
    2. Per Action alias: in a file named ActionName-alias-validation.xml
    3. Inheritance hierarchy and interfaces implemented by Action class: XWork searches up the inheritance tree of the action to find default validations for parent classes of the Action and interfaces implemented

    [...]

    In this context, "Action Alias" refers to the action name as given in the Struts configuration. Often, the name attribute matches the method name, but they may also differ.



    • Q: I also wants to use the same XML validation file for more than one method of the action .


    您可以通过多种方式进行。请记住,一个 Action 应该执行“一个操作”左右,并且在一个 Action 中不应有太多方法,请注意:
  • 如果您正在验证自定义对象(或它们的集合,例如)而不是操作中的多个单个字段,您可以使用 the Visitor Validator .它将要求您放置一个带有 ObjectClassName-validation.xml 的 XML 文件。在对象文件夹中(不是在 Action 中),并且将被每个验证它的 Action(或别名)重用。
  • 否则,您可能会弄乱层次结构,使用操作扩展并在每个级别上添加一个方法,最终创建多个 XML 验证文件,但这将是矫枉过正并且从根本上是错误的(最好在以下位置创建多个单方法操作)同级别)。

  • 然后坚持 Alias 方式,Visitor 方式,并尝试保持您的 Action 小...

    关于validation - 支柱 2 : Apply different XML validation files to one Action Methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779970/

    相关文章:

    java - Install4j 输入验证表达式

    android - EditText 验证 - 没有数字

    asp.net-mvc - DataAnnotations 或在服务中手动验证?

    struts2 - 如何创建 Java Web 应用程序的可执行文件或安装文件

    java - DataTables 的 JSON 数据中缺少根元素

    java - MobileNo 验证错误和日期时间验证技术

    java - 需要验证循环,二进制到十进制未输出正确的答案

    java - struts2中不允许显示字段错误的拦截器

    javascript - Struts 2 自动完成 onchange 事件

    css - 无法在 Struts2 中应用外部 CSS