首页 > 解决方案 > 如何从属性文件中检索列表数据?

问题描述

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

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")

公共类客户控制器 {

@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 属性吗?

标签: spring-mvchibernate-validator

解决方案


推荐阅读