0

我正在关注这个问题---> Spring MVC:如何执行验证?

这是我的控制器代码

   @RequestMapping(value = "saveBankDetails.htm", method = RequestMethod.POST)
   public ModelAndView saveBankDetails(ModelMap model ,@Valid @ModelAttribute("SpringWeb")BankDetails bean, 
           BindingResult result){
               System.out.println(result.hasErrors());
               System.out.println(bean.getNoOfBankDetails());
               System.out.println(bean.getBankDetails().get(0).getNameOfBank());
                if(result.hasErrors())
                {
                    return new ModelAndView("error/error", "command",new String());
                }
                else{
                    return new ModelAndView("bankDetails/bankDetails", "command",bean);     
                }
   }

BankDetails bean 包含 EntityBankDetails 的 ArrayList。我已将验证应用于 EntityBankDetail 的字段,如图所示

public class EntityBankDetail {
    @NotNull
    @Size(max = 3)
    private String nameOfBank;
    private Double EMIforProposedLoan;
    private String nameOfBranch;
    private String accountType;
    private String accountNumber;
    private String applicantType;
//Getters and Setters
    }

我在 BankDetail 字段中输入超过 3 个字符,以便我在控制台上获得控制器的输出为

false
3
fsdafsdfsdaf

为什么我的验证不起作用?我做错了什么?请指教..

编辑以包含 BankDetailBean

/*
 * This class represent the entire Bank Details Page. It can contain 0-4 EntityBankDetais 
 * */
public class BankDetails {

    private ArrayList<EntityBankDetail> bankDetails;

    private String[] entities;
    @Size(max = 1)
    private int noOfBankDetails;

    public ArrayList<EntityBankDetail> getBankDetails() {
        return bankDetails;
    }
    public void setBankDetails(ArrayList<EntityBankDetail> bankDetails) {
        this.bankDetails = bankDetails;
    }
    public String[] getEntities() {
        return entities;
    }
    public void setEntities(String[] entities) {
        this.entities = entities;
    }
    public int getNoOfBankDetails() {
        return noOfBankDetails;
    }
    public void setNoOfBankDetails(int noOfBankDetails) {
        this.noOfBankDetails = noOfBankDetails;
    }
}

我在这个 bean 上收到错误,无法解析 Class EntityBankDetail 键入。两者都在同一个包中,为什么会这样??

4

1 回答 1

1

当您使用 spring-mvc 时,您应该<mvc:annotation-driven />在应用程序上下文配置中指定能够在控制器中使用 JSR-303 验证。或者您可以配置您的 LocalValidatorFactoryBean 实例并将其传递给<mvc:annotation-driven validator="yourValidator" />.

于 2013-10-03T13:54:17.220 回答