我是 Struts 2 Framework 的新手,我使用 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:
- Per Action class: in a file named
ActionName-validation.xml
- Per Action alias: in a file named
ActionName-alias-validation.xml
- 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 中不应有太多方法,请注意:
Visitor Validator
.它将要求您放置一个带有 ObjectClassName-validation.xml
的 XML 文件。在对象文件夹中(不是在 Action 中),并且将被每个验证它的 Action(或别名)重用。 然后坚持 Alias 方式,Visitor 方式,并尝试保持您的 Action 小...
关于validation - 支柱 2 : Apply different XML validation files to one Action Methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779970/