spring-mvc - 如何从属性文件中检索列表数据?
问题描述
我想检查用户输入的国家是否在属性列表中。
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 属性吗?
解决方案
推荐阅读
- apache-kafka - Kafka 压缩在消费者端失败
- pine-script - Pine 编辑器中的编号栏
- graph - 卸载 TigerGraph 的开发者版或企业版的常见做法是什么?
- python-3.x - 有没有办法可以使用 groupby.sum 并保留其他列?
- javascript - 是否有用于查找元素悬停计数的 jQuery 函数?
- javascript - 如何计算 JavaScript 中关联数组的长度?
- javascript - 如何在 Angular 8 中模拟一个回调函数以使用 jasmine 测试一个 Promise
- python - 使用 sklearn ColumnTransformer 函数。参数有什么作用?
- powerbi - DAX IF 语句比较不同表中的数字?
- javascript - 如何制作酷网格的对角线。(我使用一种叫做 CodeHS 的教育工具(至少是图书馆。)