代码之家  ›  专栏  ›  技术社区  ›  Matt W

在springmvc应用程序中根据数据源(XML)验证表单输入的方法

  •  1
  • Matt W  · 技术社区  · 15 年前

    public class MyForm {
    
         private String format;
    
         public String getFormat() {
             return format;
         }
    
         public void setFormat( value:String ) {
             format = value;
         }
    }
    

    现在让我们说说 format

    public class MyFormvalidator implements Validator
    {
        @Override
        public boolean supports( Class<?> clazz ) {
            return MyForm.class.equals( clazz );
        }
    
        @Override
        public void validate( Object obj, Errors errors ) {
    
            MyForm myForm = (MyForm) obj;
            ValidationUtils.rejectIfEmptyOrWhitespace( errors, "format", "error.required" );
    
            if( !myForm.getFormat().equals( "jpg" ) &&
                !myForm.getFormat().equals( "png" ) &&
                .... etc .... ) {
               errors.rejectValue( "format", "error.invalid" );
            }
        }
    }
    

    所以这一切都很好,但假设我想添加新的支持格式,而不必调整我的验证器,重新编译和部署它。与其进入数据库,不如简单地使用一个可以修改的XML文件。它看起来像这样:

    <?xml version="1.0" encoding="UTF-8"?>
    <fileformats>
        <item>jpg</item>
        <item>png</item>
        .... etc ....
    </fileformats>
    

    2 回复  |  直到 15 年前
        1
  •  2
  •   Jacob Mattison    15 年前

    最简单的方法是使验证器成为Springbean,并将格式列表放入Spring配置中:

    public class MyFormvalidator implements Validator
    {
        private List<String> fileFormats;
    
        public MyFormValidator(List<String> fileFormats) {
            this.fileFormats = fileFormats;
        }
    
    ... //the rest of your validator, including checks for fileFormats.contains(myForm.getFormat())
    

    <bean name="myFormValidator" class="something.something.MyFormValidator">
        <constructor-arg>
             <list>
                 <value>jpg</value>
                 <value>png</value>
                 ... etc ...
             </list>
        </constructor-arg>
    </bean>
    
        2
  •  1
  •   Affe    15 年前

    您是否考虑对应用程序上下文源的更改?您可以在bean定义中创建一个列表,并将其直接注入到验证器中。

      <beans
          xmlns="http://www.springframework.org/schema/beans"
          xmlns:util="http://www.springframework.org/schema/util"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
      <bean id="myValidator"
      class="MyValidator>
          <property name="acceptedExtensions"/>
              <util:set>
                  <value>jpg</value>
                  <value>png</value>
              </util:set>
          </property> 
      </bean> 
      </beans>
    
    
      public class MyFormvalidator
      implements Validator {
          private Set<String> acceptedExtensions;
          public void setAcceptExtensions(Set<String> extensions) {//etc}
    
          @Override
          public void validate( Object obj, Errors errors ) {
    
              MyForm myForm = (MyForm) obj;
              ValidationUtils.rejectIfEmptyOrWhitespace(
                  errors, "format", "error.required" );
    
              if( !acceptedExtensions.contains(myForm.getFormat){
                 errors.rejectValue( "format", "error.invalid" );
              }
          }