Java Thymeleaf @ModelAttribute 控制器值为 null,即使 th:object 已指定

问题描述 投票:0回答:1

我在提交表单时无法使用 Spring + Thymeleaf 解决问题。 我在加载页面时创建一个新的 dto,并通过 th:field 更新它。 但是,当我提交表单时,我发现所有 dto 值均为空。 我知道有很多问题问这个问题,但他们的解决方案似乎都不适合我。

这是我的表格:

 <form action="/api/v2/transaction/recurring/add" method="post" th:object="${rtdto}">
                            <select name="category" id="Tcategory" required th:field="*{category}">
                                <option value="NONE" disabled selected hidden>Category</option>
                                <option value="HOUSING">Housing</option>
                                <option value="TRANSPORTATION">Transportation</option>
                                <option value="FOOD">Food/Drink</option>
                                <option value="UTILITIES">Utilities</option>
                                <option value="CLOTHING">Clothing</option>
                                <option value="MEDICAL_HEALTHCARE">Medical/Healthcare</option>
                                <option value="INSURANCE">Insurance</option>
                                <option value="HOUSEHOLD_ITEMS_SUPPLIES">Household Items/Supplies</option>
                                <option value="PERSONAL">Personal</option>
                                <option value="ENTERTAINMENT">Entertainment</option>
                                <option value="SUBSCRIPTION">Subscriptions</option>
                                <option value="GROCERY">Grocery</option>
                                <option value="MISCELLANEOUS">Miscellaneous</option>
                            </select>
                            <input type="text" required name="name" placeholder="Name" autocomplete="off" autocapitalize="off" spellcheck="false" th:field="*{name}">
                            <input type="number" step="0.01" required name="amt" placeholder="Amount" autocomplete="off" min="0.01" th:field="*{amount}">
                            <select name="freq" id="freq" required th:field="*{period}">
                                <option value="NONE" disabled selected hidden>Frequency</option>
                                <option value="MONTH">Every Month</option>
                                <option value="YEAR">Every Year</option>
                            </select>
                            <input type="hidden" name="callback" th:value="${backHREF}">
                            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                            <input type="submit" class="button" value="Add">
                        </form>

这是我初始化 DTO 的控制器代码:

@GetMapping("/settings/{callback}")
    @PreAuthorize("isAuthenticated() and hasRole('VERIFIED') and hasRole('COMPLETED')")
    public ModelAndView getSettings(@PathVariable(value = "callback", required = false) String callback, Model model) {
        ModelAndView mav = new ModelAndView("settings");
        Account account = userService.getAccount();
        mav.addObject("cssPath", Utils.Paths.Styles.INDEX_CSS_PATH)
                .addObject("backHREF", callback.equals("main") ? "" : callback)
                .addObject("accDTO", new SettingsAccountDTO(account.getFirstName(), account.getLastName(), account.getEmail(), account.getCountry()))
                .addObject("firstName", account.getFirstName())
                .addObject("recurringTransactions", transactionService.getAllRecurringTransactions())
                .addObject("rtdto", new RecurringTransactionDTO());
        if (!model.containsAttribute("message") && !model.containsAttribute("color")) {
            mav.addObject("error", "&nbsp;")
                    .addObject("color", Color.LIGHT_BACKGROUND.getColor());
        }
        if (!model.containsAttribute("messageB") && !model.containsAttribute("colorB")) {
            mav.addObject("errorB", "&nbsp;")
                    .addObject("colorB", Color.LIGHT_BACKGROUND.getColor());
        }
        if (!model.containsAttribute("messageU") && !model.containsAttribute("colorU")) {
            mav.addObject("errorU", "&nbsp;")
                    .addObject("colorU", Color.LIGHT_BACKGROUND.getColor());
        }
        if (!model.containsAttribute("messageRT") && !model.containsAttribute("colorRT")) {
            mav.addObject("errorRT", "&nbsp;")
                    .addObject("colorRT", Color.LIGHT_BACKGROUND.getColor());
        }
        return mav;
    }

最后,这是提交表单时使用的@PostMapping:

@PostMapping("/recurring/add")
    @PreAuthorize("isAuthenticated() and hasRole('COMPLETED') and hasRole('VERIFIED')")
    public ModelAndView addRecurringTransaction(RedirectAttributes attr, @ModelAttribute RecurringTransactionDTO dto, @RequestParam("callback") String callback, Model model) {
        log.info(dto.getCategory() + dto.getName() + dto.getPeriod() + dto.getAmount());
        return new ModelAndView("redirect:/settings/" + (callback.equals("") ? "main" : callback) + "#charges");
    }

这是结果:

nullnullnullnull

如有任何帮助,我们将不胜感激!

java html spring forms thymeleaf
1个回答
0
投票

提交类型从名称属性中获取值。 使用表单提交时,需要声明 name 属性,与类的有效声明相同。

© www.soinside.com 2019 - 2024. All rights reserved.