如何从属性文件中检索列表数据?

时间:2018-12-07 21:20:50

标签: spring-mvc hibernate-validator

我想检查用户输入的国家/地区是否在属性列表中。

public class CountryValidator implements ConstraintValidator<CountryValid,String> {

@Value("#{countryOptions}")
Map<String, String> countryOptions;


@Override
public boolean isValid(String girilenDeger, ConstraintValidatorContext arg1) {
    // TODO Auto-generated method stub


        return countryOptions.containsKey(girilenDeger);


}

@Override
public void initialize(CountryValid constraintAnnotation) {
    // TODO Auto-generated method stub
    ConstraintValidator.super.initialize(constraintAnnotation);

}

}

但是,我之前在控制器类中已经成功使用了此列表。在验证类中再次使用它时,出现NullPointerException错误。

@Controller@RequestMapping("/customerForm")

公共类CustomerController {

@Value("#{countryOptions}")
Map<String, String> countryOptions;

@RequestMapping("/mainMenu")
public String returnForm(Model model) {

    model.addAttribute("theCountryOptions", countryOptions);

    Customer customer1 = new Customer();
    model.addAttribute("customer1", customer1);

    return "customer-view/main-menu";
}

@RequestMapping("/resultPage")
public String returnResult(@Valid @ModelAttribute("customer1") Customer customer, BindingResult result,
        Model model) {

    model.addAttribute("theCountryOptions", countryOptions);


    if (result.hasErrors())
        return "customer-view/main-menu";
    else {

        AddDatabase<Customer> database = new AddDatabase<Customer>();
        database.setObj(customer);
        database.addData();
        System.out.println("Ekleme islemi tamamlandı.");

        return "customer-view/result-page";
    }
}

}

还是可以从模型中检索CountryOptions属性?

0 个答案:

没有答案