You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在application.properties存储数组并在Thymeleaf中读取展示

具体实现步骤

整个实现全程不需要在代码/模板里硬编码血型枚举值,后续调整选项只需要改配置文件即可。

1. 在application.properties中定义血型配置

直接用逗号分隔所有可选血型值,Spring会自动识别并转换为列表类型:

# 可选血型列表,逗号分隔,不要加多余空格
app.blood-type-list=A型,B型,AB型,O型,Rh阴性A型,Rh阴性B型,Rh阴性AB型,Rh阴性O型

2. 编写配置绑定类接收配置值

用Spring Boot的配置绑定特性,把配置文件里的列表自动映射成Java对象,方便全局注入使用:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@ConfigurationProperties(prefix = "app")
public class AppCommonConfig {
    private List<String> bloodTypeList;

    // 没集成Lombok就手动写getter/setter
    public List<String> getBloodTypeList() {
        return bloodTypeList;
    }

    public void setBloodTypeList(List<String> bloodTypeList) {
        this.bloodTypeList = bloodTypeList;
    }
}

如果项目用了Lombok,直接给类加@Data注解就不用手写getter/setter。

3. 将配置列表传递给Thymeleaf视图

两种传值方式选一种即可:

  • 单页面使用:直接在对应接口方法里把列表塞进Model
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class UserFormController {
    
        private final AppCommonConfig appCommonConfig;
    
        // 构造器注入,Spring 4.3+不需要额外加@Autowired
        public UserFormController(AppCommonConfig appCommonConfig) {
            this.appCommonConfig = appCommonConfig;
        }
    
        @GetMapping("/user/info/edit")
        public String toEditPage(Model model) {
            // 其他业务参数传递...
            model.addAttribute("bloodTypeList", appCommonConfig.getBloodTypeList());
            return "user/edit";
        }
    }
    
  • 多页面全局使用:写个全局模型增强类,所有视图都能自动拿到这个列表,不用每个接口重复传
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import java.util.List;
    
    @ControllerAdvice
    public class GlobalViewModelConfig {
    
        private final AppCommonConfig appCommonConfig;
    
        public GlobalViewModelConfig(AppCommonConfig appCommonConfig) {
            this.appCommonConfig = appCommonConfig;
        }
    
        @ModelAttribute("bloodTypeList")
        public List<String> globalBloodTypeList() {
            return appCommonConfig.getBloodTypeList();
        }
    }
    

4. Thymeleaf模板渲染下拉选择框

直接遍历传过来的列表生成option标签即可:

<div class="form-group">
    <label for="bloodType">选择血型:</label>
    <select id="bloodType" name="bloodType" class="form-control">
        <option value="">请选择血型</option>
        <option th:each="item : ${bloodTypeList}"
                th:value="${item}"
                th:text="${item}">
        </option>
    </select>
</div>

如果要做编辑态的默认选中,给option标签加选中判断就行:

<option th:each="item : ${bloodTypeList}"
        th:value="${item}"
        th:text="${item}"
        th:selected="${user.bloodType == item}">
</option>

补充说明

  • 后续需要调整血型选项、增删枚举值的时候,直接修改application.properties里的配置项,重启服务就生效,不需要改业务代码或者模板代码,完全避免硬编码。
  • 如果配置值里有特殊字符,按照properties文件的转义规则处理即可。
  • 不要在Controller或者模板里直接写死血型数组,不然后续调整要改代码重新打包,失去配置化的意义。

内容的提问来源于stack exchange,提问作者michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 13:24:22