1

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";
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
mmathank
  • 23
  • 5

1 Answers1

1

If you want to use genderOptions as Map in the Controller, then first you need specify it in the form of key-value in gender.properties file.

genderOptions = {M:'Male', F:'Female'}

And while accessing it in the 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;
Anish B.
  • 9,111
  • 3
  • 21
  • 41
Reeta Wani
  • 197
  • 4
  • 13
  • >Its working. I'm getting the values from properties file after making changes as you suggested. But, I'm facing one more issue while displaying those options (read from propertied) in form. ' ' – mmathank Apr 05 '20 at 14:16
  • 1
    > Its working fine. after adding this.. I got the expected output. Thanks for your input. – mmathank Apr 05 '20 at 14:23