【问题标题】:Setting values in thymeleaf using a map使用地图在 thymeleaf 中设置值
【发布时间】:2021-12-28 11:39:23
【问题描述】:

I 从地图生成一个复选框列表。现在如何设置键的值(false / true),现在我可以在 UserConfig 中下载它,以便我可以在项目的其余部分使用这个值。

我的看法:

<body>
<main>
    <form th:action="@{/file/uploadFile}" enctype="multipart/form-data" method="POST"/>
    <fieldset>
        <legend>Generate Report</legend>
            <label> Upload File
                <input name="file" type="file" required/>
                <input type="submit" value="Upload"/>
            </label>
            <label th:each="item : ${userConfig.isEnableMap}">
                <input type="checkbox" id="" th:text="${item.key}" th:value="${item.value}"/>
            </label>
        </form>
    </fieldset>
</main>
</body>

我的班级用户配置:

    @Component
public class UserConfig {

    private Map<String, Boolean> isEnableMap = new HashMap<>();

    public UserConfig() {
        isEnableMap.put(EnableProcess.MONTHLY_TIME_UPDATE.getName(), false);
        isEnableMap.put(EnableProcess.SUM.getName(), false);
        isEnableMap.put(EnableProcess.HIDE_COLUMNS.getName(), true);
    }

    public UserConfig(Map<String, Boolean> isEnableMap) {
        this.isEnableMap = isEnableMap;
    }

    public Map<String, Boolean> getIsEnableMap() {
        return isEnableMap;
    }

    public void setIsEnableMap(Map<String, Boolean> isEnableMap) {
        this.isEnableMap = isEnableMap;
    }


    public enum EnableProcess {
        MONTHLY_TIME_UPDATE("Monthly time update"), SUM("Sum"), HIDE_COLUMNS("Hide columns");

        private final String name;

        EnableProcess(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

控制器

  @PostMapping(value = "/uploadFile", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<Resource> uploadFile(@RequestParam("file") MultipartFile file, @ModelAttribute("userConfig") UserConfig userConfig) {
    String fileName = file.getOriginalFilename();
    if (getExtension(fileName).equals("XLSX") || getExtension(fileName).equals("XLS")) {
        XSSFWorkbook workbook = reportService.processFile(file);
        reportService.writeWorkbook(workbook);
    }
    Resource resource = new ClassPathResource("temp/" + reportConst.getTempFileName());
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + reportConst.getTempFileName() + "\"");
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}

我不使用数据库。它只需要保存值以生成报告

【问题讨论】:

  • 在控制器中,然后:userConfig.getIsEnableMap().get("MONTHLY_TIME_UPDATE") !? ..and SUM (..and all keys, you want to "check checked":)
  • 也重命名模型属性会有所帮助,以区分(只读)${userConfig...}(生成复选框)和(选中)*{userConfig}(字段,相对来自提交的“表单对象”(th:object,如果有的话))。
  • 是否可以一次完成,例如迭代?所以我不必编写每个变量并以某种方式遍历地图?

标签: java spring-boot spring-thymeleaf


【解决方案1】:

使用Preprocessing(如果我没听错的话),我们可以尝试以下方法:

<input th:field="*{userConfig.isEnableMap['__${item.key}__']}" ... />

...假设其余的工作正常。 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 2020-03-20
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多