5

I Generates a checkbox list from the map. Now how to set the value for the key (false / true) and now I can download it in UserConfig so that I can use this value in the rest of the project.

My view:

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

My class UserConfig :

    @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;
        }
    }

Controller

  @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);
}

I do not use the database. It only needs the values to be saved for the purpose of generating the report

Dev007
  • 366
  • 1
  • 12
  • in controller, then: `userConfig.getIsEnableMap().get("MONTHLY_TIME_UPDATE")` !? ..and `SUM` (..and all keys, you want to "check checked":) – xerx593 Dec 28 '21 at 15:44
  • also re-naming model attributes would be helpful, to distinguish between the (read-only) `${userConfig...}` (to generate the checkboxes), and the (checked) `*{userConfig}` (field, relatively from the "form object" (th:object, if any)) submitted. – xerx593 Dec 28 '21 at 15:51
  • Is it possible to do it at once, e.g. with iteration? So I don't have to write every variable and do it somehow iterating through the map? – Dev007 Dec 28 '21 at 16:41

1 Answers1

2

With Preprocessing (if I got you right), we could try something like:

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

... assuming the rest works. ;)

xerx593
  • 12,237
  • 5
  • 33
  • 64