Spring MVC – 使用Java配置读取属性文件

huangapple go评论84阅读模式
英文:

Spring MVC - Reading properties file using Java Configuration

问题

由于异常无法读取 .properties 文件中的值(org.springframework.expression.spel.SpelEvaluationException: EL1008E: 无法找到属性或字段 'genderOptions')

我已配置属性占位符。我的属性文件中有两个条目(M=MALE,F=FEMALE),我想在提交表单时将其作为复选框的选项列表填充。

  1. @Bean
  2. public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
  3. return new PropertySourcesPlaceholderConfigurer();
  4. }
  5. @Controller
  6. @RequestMapping("/player")
  7. @PropertySource(ignoreResourceNotFound = true, value = "classpath:gender.properties")
  8. public class PlayerController {
  9. @Value("#{genderOptions}")
  10. public Map<String, String> genderOptions;
  11. @RequestMapping("/playerForm")
  12. public String showPlayerForm(Model model) {
  13. Player player = new Player();
  14. model.addAttribute("player", player);
  15. model.addAttribute("genderOptions", genderOptions);
  16. return "player-form";
  17. }
  18. }
英文:

> 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.

  1. @Bean
  2. public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
  3. return new PropertySourcesPlaceholderConfigurer();
  4. }
  5. @Controller
  6. @RequestMapping(&quot;/player&quot;)
  7. @PropertySource(ignoreResourceNotFound = true, value =
  8. &quot;classpath:gender.properties&quot;)
  9. public class PlayerController {
  10. @Value(&quot;#{genderOptions}&quot;)
  11. public Map&lt;String, String&gt; genderOptions;
  12. @RequestMapping(&quot;/playerForm&quot;)
  13. public String showPlayerForm(Model model) {
  14. Player player = new Player();
  15. model.addAttribute(&quot;player&quot;, player);
  16. model.addAttribute(&quot;genderOptions&quot;, genderOptions);
  17. return &quot;player-form&quot;;
  18. }

答案1

得分: 1

如果您想在控制器中使用genderOptions作为Map,则首先需要在gender.properties文件中以键值对的形式进行指定。

  1. genderOptions = {M:&#39;Male&#39;, F:&#39;Female&#39;}

在控制器中访问时,您需要进行以下更改,以便让Spring将其转换为Map。

  1. @Value(&quot;#{${genderOptions}}&quot;)
  2. private Map&lt;String, String&gt; mapValues;

如果您需要获取Map中特定键的值,您只需将键的名称添加到表达式中:

  1. @Value(&quot;#{${genderOptions}.M}&quot;)
  2. private String maleKey;
英文:

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:&#39;Male&#39;, F:&#39;Female&#39;}

And while accessing it in the controller, you need to make following changes in order to let spring cast it in Map.

  1. @Value(&quot;#{${genderOptions}}&quot;)
  2. private Map&lt;String, String&gt; 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:

  1. @Value(&quot;#{${genderOptions}.M}&quot;)
  2. private String maleKey;

huangapple
  • 本文由 发表于 2020年4月5日 17:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61040588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定