由于异常而无法读取.properties文件中的值(org.springframework.expression.spel.SpelEvaluationException:EL1008E:找不到属性或字段“ genderOptions”)
我已经配置了财产占位符。我的属性文件有两个条目(M = MALE,F = FEMALE),我想在提交表单时将其填充为复选框中的选项列表。
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Controller
@RequestMapping("/player")
@PropertySource(ignoreResourceNotFound = true, value =
"classpath:gender.properties")
public class PlayerController {
@Value("#{genderOptions}")
public Map<String, String> genderOptions;
@RequestMapping("/playerForm")
public String showPlayerForm(Model model) {
Player player = new Player();
model.addAttribute("player", player);
model.addAttribute("genderOptions", genderOptions);
return "player-form";
}
genderOptions={M:'Male',F:'Female'}
并且在控制器中访问它时,您需要进行以下更改以让Spring将其投射到Map中
@Value("#{${genderOptions}}")
private Map<String,String> mapValues;