java thymeleaf@modeldattribute控制器值为空,即使已指定th:object

xriantvc  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(149)

在提交表单时,我无法解决spring+thymeleaf的问题。我在加载页面时创建一个新的dto,并通过th:字段更新它。但是,当我提交表单时,我发现所有的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

任何帮助都将不胜感激!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题