Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting values in thymeleaf using a map

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

like image 788
Dev007 Avatar asked Oct 26 '22 09:10

Dev007


1 Answers

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

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

... assuming the rest works. ;)

like image 150
xerx593 Avatar answered Nov 26 '22 18:11

xerx593