<tfoot id='tpNim'></tfoot>
    1. <legend id='tpNim'><style id='tpNim'><dir id='tpNim'><q id='tpNim'></q></dir></style></legend>
    2. <i id='tpNim'><tr id='tpNim'><dt id='tpNim'><q id='tpNim'><span id='tpNim'><b id='tpNim'><form id='tpNim'><ins id='tpNim'></ins><ul id='tpNim'></ul><sub id='tpNim'></sub></form><legend id='tpNim'></legend><bdo id='tpNim'><pre id='tpNim'><center id='tpNim'></center></pre></bdo></b><th id='tpNim'></th></span></q></dt></tr></i><div id='tpNim'><tfoot id='tpNim'></tfoot><dl id='tpNim'><fieldset id='tpNim'></fieldset></dl></div>

      <small id='tpNim'></small><noframes id='tpNim'>

          <bdo id='tpNim'></bdo><ul id='tpNim'></ul>
      1. 在 Struts 2 中重用自定义表达式验证器

        Reusing Custom Expression Validator in Struts 2(在 Struts 2 中重用自定义表达式验证器)
        <legend id='18fEp'><style id='18fEp'><dir id='18fEp'><q id='18fEp'></q></dir></style></legend>

        <i id='18fEp'><tr id='18fEp'><dt id='18fEp'><q id='18fEp'><span id='18fEp'><b id='18fEp'><form id='18fEp'><ins id='18fEp'></ins><ul id='18fEp'></ul><sub id='18fEp'></sub></form><legend id='18fEp'></legend><bdo id='18fEp'><pre id='18fEp'><center id='18fEp'></center></pre></bdo></b><th id='18fEp'></th></span></q></dt></tr></i><div id='18fEp'><tfoot id='18fEp'></tfoot><dl id='18fEp'><fieldset id='18fEp'></fieldset></dl></div>

            <tbody id='18fEp'></tbody>
          1. <tfoot id='18fEp'></tfoot>
            • <small id='18fEp'></small><noframes id='18fEp'>

                <bdo id='18fEp'></bdo><ul id='18fEp'></ul>
                • 本文介绍了在 Struts 2 中重用自定义表达式验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Struts 2 中我们可以开发 @CustomValidator 可以在应用范围内使用

                  In Struts 2 we can develop @CustomValidator which can be used in application wide

                  @CustomValidator(type = "CustomerNumberValidator", fieldName = "customerNo")
                  

                  为了验证更多,我们使用@ExpressionValidator

                  @ExpressionValidator(expression = 
                  "( (!''.equals(account.firstName) && (!''.equals(account.lastName) )
                     || (presonalAccount == false)", 
                     key = "validate.account.name")
                  

                  如果表达式太复杂,需要处理更多个字段,我们使用OGNL调用静态方法.静态方法将进行验证并返回一个 boolean 例如

                  If the expression is too complicated and needs to work on more than one field we use OGNL to call the static method. The static method will do the validation and return a boolean for example

                  @ExpressionValidator(expression = "@foo.bar.CalendarUtil@compareDates(fromDate,toDate)", key = "validate.date.before")
                  

                  以上是自定义表达式验证器的一些方法!

                  Above is some how a Custom Expression Validator !

                  我们在应用程序范围内使用 @foo.bar.CalendarUtil@compareDates 为我们进行此验证.

                  And we use @foo.bar.CalendarUtil@compareDates in application wide to make this validation for us.

                  还有其他方法可以让我们使用自定义范围验证器吗?!

                  Is there another approach which enables us to use a custom wide validator?!

                  是否有任何自定义表达式验证器可以添加到 Struts 中,我们可以像使用 @CustomValidator 一样在 Action 中调用它?

                  Is there any custom expression validator which can be added to Struts and we can call it in Action in the way we use @CustomValidator?

                  推荐答案

                  创建自定义验证器(与字段无关):

                  Create a Custom Validator (not field related):

                  public final class CompareDatesValidator extends ValidatorSupport {
                      private String fromDate; // getter and setter
                      private String toDate;   // getter and setter    
                  
                      @Override
                      public void validate(Object o) throws ValidationException {
                          Date d1 = (Date)parse(fromDate, Date.class);
                          Date d2 = (Date)parse(toDate, Date.class);
                  
                          if (d1==null || d2==null || d2.before(d1)){
                              addActionError(getDefaultMessage());
                          }
                      }
                  }
                  

                  validators.xml 文件中注册自定义验证器:

                  Register the custom validator in validators.xml file:

                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE validators PUBLIC
                       "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
                       "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
                  <validators>
                      <validator name="compareDatesValidator" 
                                class="org.foo.bar.CompareDatesValidator"/>
                  </validators>
                  

                  在操作中使用验证器:

                  private Date startDate; // getter and setter
                  private Date endDate;   // getter and setter
                  
                  @Validations(
                      customValidators={
                          @CustomValidator(type="compareDatesValidator", 
                              message="Dates provided are not valid."
                              parameters={
                                  @ValidationParameter(name="fromDate", value="${startDate}"), 
                                  @ValidationParameter(name="toDate",   value="${endDate}")})})
                  public String execute(){
                      return SUCCESS;
                  }
                  

                  这篇关于在 Struts 2 中重用自定义表达式验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)
                    <legend id='sLAQJ'><style id='sLAQJ'><dir id='sLAQJ'><q id='sLAQJ'></q></dir></style></legend>

                      <bdo id='sLAQJ'></bdo><ul id='sLAQJ'></ul>

                        <small id='sLAQJ'></small><noframes id='sLAQJ'>

                          <tbody id='sLAQJ'></tbody>
                          <i id='sLAQJ'><tr id='sLAQJ'><dt id='sLAQJ'><q id='sLAQJ'><span id='sLAQJ'><b id='sLAQJ'><form id='sLAQJ'><ins id='sLAQJ'></ins><ul id='sLAQJ'></ul><sub id='sLAQJ'></sub></form><legend id='sLAQJ'></legend><bdo id='sLAQJ'><pre id='sLAQJ'><center id='sLAQJ'></center></pre></bdo></b><th id='sLAQJ'></th></span></q></dt></tr></i><div id='sLAQJ'><tfoot id='sLAQJ'></tfoot><dl id='sLAQJ'><fieldset id='sLAQJ'></fieldset></dl></div>
                          • <tfoot id='sLAQJ'></tfoot>