2013-11-15 11 views
0

타원으로 유효성 검사를하고 있습니다. 나는 스프링 배치를 사용하여 객체를 채우고 객체를 타원으로 유효성을 검사합니다. 하지만 내 @ javax.validation.constraints.Pattern 작동하지 않습니다, 내 콘솔 프로그램을 실행할 때 오류를 표시하지 않기 때문에 생각합니다. 이 내 CSV입니다타원형 유효성 검사 프레임 워크 - @ javax.validation.constraints.Pattern 구현 (작동하지 않음)

package com.mistaiker; 

import org.springframework.batch.item.file.mapping.FieldSetMapper; 
import org.springframework.batch.item.file.transform.FieldSet; 
import org.springframework.validation.BindException; 

public class ClientFieldSetMapper implements FieldSetMapper<Client>{ 

    @Override 
    public Client mapFieldSet(FieldSet fieldSet) throws BindException { 

     Client client = new Client(); 

     client.setTitle(fieldSet.readRawString(0)); 
     client.setFirstName(fieldSet.readRawString(1)); 
     client.setSurname(fieldSet.readRawString(2)); 
     client.setRegNum(fieldSet.readLong(3)); 
     client.setAssigment_1(fieldSet.readInt(4)); 
     client.setAssigment_2(fieldSet.readInt(5)); 
     client.setAssigment_3(fieldSet.readInt(6)); 
     client.setAddress(fieldSet.readRawString(7)); 

     return client; 
    } 

} 

App.java

package com.mistaiker; 

import java.util.ArrayList; 
import java.util.List; 

import net.sf.oval.ConstraintViolation; 
import net.sf.oval.Validator; 

import org.springframework.batch.item.ExecutionContext; 
import org.springframework.batch.item.ParseException; 
import org.springframework.batch.item.UnexpectedInputException; 
import org.springframework.batch.item.file.FlatFileItemReader; 
import org.springframework.batch.item.file.mapping.DefaultLineMapper; 
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; 
import org.springframework.core.io.FileSystemResource; 

public class App { 

    public static void main(String[] args){ 

     FlatFileItemReader<Client> itemReader = new FlatFileItemReader<Client>(); 
     itemReader.setResource(new FileSystemResource("C:/Users/juan.m.palazzolo/Desktop/data.csv")); 

     DefaultLineMapper<Client> lineMapper = new DefaultLineMapper<Client>(); 
     lineMapper.setLineTokenizer(new DelimitedLineTokenizer()); 
     lineMapper.setFieldSetMapper(new ClientFieldSetMapper()); 
     itemReader.setLineMapper(lineMapper); 
     itemReader.open(new ExecutionContext()); 

     Client client = new Client(); 
     ArrayList<Client> clients = new ArrayList<>(); 

     try { 
      client = itemReader.read(); 

      //validation!! 
      Validator validator = new Validator();//Validator object 
      List<ConstraintViolation> violations = validator.validate(client); 


      if(violations.size()>0) 
      { 
//    Log.severe("Object " + client + " is invalid."); 
//    throw new BussinessException(violations); 
       System.out.println("error en validacion"); 

      } 



      while(client != null){ 

       client.showData(); 

       clients.add(client); 

       client = itemReader.read(); 

       if(client != null){ 
       violations = validator.validate(client);} 


       //validation!! 


       if(violations.size()>0) 
       { 
        //LOG.severe("Object " + client + " is invalid."); 
        //throw new BussinessException(violations); 
        System.out.println("error en validacion"); 
       } 
      } 

     } catch (UnexpectedInputException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     /*for(Client x: clients){ 
      x.showData(); 
     }*/ 

    } 


} 

Client.jaba

package com.mistaiker; 



import net.sf.oval.constraint.Length; 
import net.sf.oval.constraint.MinLength; 
import net.sf.oval.constraint.NotEmpty; 
import net.sf.oval.constraint.NotNegative; 
import net.sf.oval.constraint.NotNull; 


public class Client { 

    //Atributos 
    @NotNull 
    @NotEmpty 
    @MinLength(value = 1) 
    @Length(max=4) 
    @javax.validation.constraints.Pattern(regexp = "[a-zA-Z]") 
    private String title; 

    @NotNull 
    @NotEmpty 
    @MinLength(value = 1) 
    @Length(max=20) 
    @javax.validation.constraints.Pattern(regexp = "[a-zA-Z]") 
    private String firstName; 

    @NotNull 
    @NotEmpty 
    @MinLength(value = 1) 
    @Length(max=20) 
    @javax.validation.constraints.Pattern(regexp = "^[a-zA-Z]") 
    private String surname; 

    @NotNull 
    @NotEmpty 
    @NotNegative 
    @MinLength(value = 10) 
    @Length(max=10) 
    @javax.validation.constraints.Pattern(regexp = "[0-9]") 
    private long regNum; 

    @NotNull 
    @NotEmpty 
    @NotNegative 
    @MinLength(value = 2) 
    @Length(max=2) 
    @javax.validation.constraints.Pattern(regexp = "[0-9]") 
    private int assigment_1; 

    @NotNull 
    @NotEmpty 
    @NotNegative 
    @MinLength(value = 2) 
    @Length(max=2) 
    @javax.validation.constraints.Pattern(regexp = "[0-9]") 
    private int assigment_2; 

    @NotNull 
    @NotEmpty 
    @NotNegative 
    @MinLength(value = 2) 
    @Length(max=2) 
    @javax.validation.constraints.Pattern(regexp = "[0-9]") 
    private int assigment_3; 

    @NotNull 
    @NotEmpty 
    @MinLength(value = 1) 
    @Length(max=30) 
    @javax.validation.constraints.Pattern(regexp = "[0-9a-zA-Z]") 
    private String address; 


    //metodos 
    void showData(){ 
     System.out.println(title + "\t" + firstName + "\t" + surname + "\t" + 
          regNum + "\t" + assigment_1 + "\t" + assigment_2 + "\t" + 
          assigment_3 + "\t" + address); 
    } 

    //Getters y setters 
    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getSurname() { 
     return surname; 
    } 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 
    public long getRegNum() { 
     return regNum; 
    } 
    public void setRegNum(long regNum) { 
     this.regNum = regNum; 
    } 
    public int getAssigment_1() { 
     return assigment_1; 
    } 
    public void setAssigment_1(int assigment_1) { 
     this.assigment_1 = assigment_1; 
    } 
    public int getAssigment_2() { 
     return assigment_2; 
    } 
    public void setAssigment_2(int assigment_2) { 
     this.assigment_2 = assigment_2; 
    } 
    public int getAssigment_3() { 
     return assigment_3; 
    } 
    public void setAssigment_3(int assigment_3) { 
     this.assigment_3 = assigment_3; 
    } 
    public String getAddress() { 
     return address; 
    } 
    public void setAddress(String address) { 
     this.address = address; 
    } 



} 

ClientFieldSetMapper.java : 여기

내 clases 있습니다

012 3,516,
Mr,juan,mik2e,1234567891,75,84,80,Flat 1a\\The Street\\The Town 
Mr,John,Smith,1234567892,68,57,72,Flat 1b\\The Street\\The Town 
Mr,Zack,Watts,1234567893,32,43,49,Flat 1c\\The Street\\The Town 
Mrs,Hillary,Rix,1234567894,87,88,86,Flat 1d\\The Street\\The Town 
Miss,Zoe,Adams,1234567895,52,48,57,Flat 1e\\The Street\\The Town 
Mr,Andy,Brown,1234567896,42,52,54,Flat 1f\\The Street\\The Town 
+0

에 설명 된대로 콩 검증 제약 조건의 해석을 활성화해야합니다 그리고 왜해야합니까? 패턴은 숫자, 날짜 등이 아닌 텍스트 기반 속성에서만 작동합니다. 따라서 기본적으로'String'에서 작동합니다. 당신의 다른 사람들은 거의 쓸모가 없습니다. –

답변