Values from .properties file could not read due to exception (org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'genderOptions' cannot be found)
I have configured the property place holder. My property file is having two entries (M=MALE, F=FEMALE) I wanted to populate this as a list of options in checkbox while submitting the form.
@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";
}
If you want to use genderOptions as Map in Controller first you need specify it in following syntax in your gender.properties file.
genderOptions={M:'Male',F:'Female'}
And while accessing it in controller you need to make following changes in order to let spring cast it in Map
@Value("#{${genderOptions}}")
private Map<String,String> mapValues;
And if you need to get the value of a specific key in the Map, all you have to do is add the key's name in the expression:
@Value("#{${genderOptions}.M}")
private String maleKey;
Hope this helps.