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

问题描述 投票:0回答:1

由于异常而无法读取.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";
}
java spring spring-mvc jsp el
1个回答
0
投票
genderOptions={M:'Male',F:'Female'}

并且在控制器中访问它时,您需要进行以下更改以让Spring将其投射到Map中

@Value("#{${genderOptions}}") private Map<String,String> mapValues;

© www.soinside.com 2019 - 2024. All rights reserved.