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>
我的類 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;
}
}
控制器
@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);
}
我不使用資料庫。它只需要為了生成報告而保存的值
uj5u.com熱心網友回復:
使用預處理(如果我沒猜錯的話),我們可以嘗試以下操作:
<input th:field="*{userConfig.isEnableMap['__${item.key}__']}" ... />
...假設其余的作業。;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/397763.html
