dto未被传递(spring-mvc)

mwkjh3gx  于 2021-07-22  发布在  Java
关注(0)|答案(1)|浏览(304)

我正在为我的网站创建一个注册页,它提交罚款。当电子邮件已经存在时,我希望它重新创建名、姓、电子邮件和国家/地区中的值。问题是它显示的文本很好,但是值不会被重新创建。
我的jsp文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
    <!--/*@thymesVar id="cssPath" type="java.lang.String"*/-->
    <link rel="stylesheet" th:href="${cssPath}">
</head>
<body>

    <div class="grayDiv divCenter">
        <h1 th:text="${title}" class="titleTextCenter"></h1>
        <!--/*@thymesVar id="error" type="java.lang.String"*/-->
        <!--/*@thymesVar id="color" type="java.lang.String"*/-->
        <h2 th:text="${error}" class="textCenter text" th:style="'color: '+${color}+';'"></h2>
        <div>
            <form th:action="@{/api/v2/signup/register}" method="post" class="signup" style="width:100%;" th:object="${account}" th:style="'width:100%;'">
                <div>
                    <input th:field="*{firstName}" type="text" placeholder="First Name" class="formTextBox" style="width: 48%; float: left;" pattern="[a-zA-Z]*" required name="fname">
                    <input th:field="*{lastName}" type="text" placeholder="Last Name" class="formTextBox" style="width: 48%; float: right;" pattern="[a-zA-Z]*" required name="lname" >
                </div>
                <br>
                <input th:field="*{email}" type="email" placeholder="Email" class="formTextBox"  required name="email" >
                <div style="width: 100%;">
                    <select th:field="*{country}" name="country" id="country" class="formTextBox formSelect" required>
                        <option value="" disabled hidden style="color: #8e8e8e;">Country</option>
                        <option value="US">United States</option>
                        <option value="INDIA">India</option>
                        <option value="UK">United Kingdom</option>
                        <option value="EU">European Union</option>
                        <option value="AUS">Australia</option>
                        <option value="JAPAN">Japan</option>
                        <option value="CANADA">Canada</option>
                    </select>
                </div>
                <input th:field="*{password}" type="password" placeholder="Password" class="formTextBox" required name="pwrd" id="pwrd" >
                <input th:field="*{confirmPassword}" type="password" placeholder="Confirm Password" class="formTextBox" required name="cpwrd" id="cpwrd">
                <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                <input type="submit" name="submit" value="Next" class="button">
            </form>
        </div>
    </div>

</body>
</html>

这是我的@getmapping注册页面:

@GetMapping("/signup")
@PreAuthorize("permitAll()")
public String signup(Model model, @RequestParam(value = "color", required = false) String color, @RequestParam(value = "error", required = false) String error, AccountDTO dto) {
    log.info(dto.getEmail());
    model.addAttribute("account", new AccountDTO());
    model.addAttribute("cssPath", "../css/index.css");
    model.addAttribute("title", "Welcome To Expense Tracker");
    model.addAttribute("error", error == null ?  "." : error);
    model.addAttribute("color", color == null ? Color.LIGHT_BACKGROUND.getColor() : color);
    return "signup/signup";
}

最后是我的注册api:

@PostMapping("register")
public ModelAndView register(@ModelAttribute("account") @Valid AccountDTO dto, HttpServletRequest request) {
    log.info("Registering user");
    log.info("email: " + dto.getEmail());
        try {
            log.info("Checking if user exists");
            Account account = service.register(dto);
            String url = request.getContextPath();
            eventPublisher.publishEvent(new RegistrationCompleteEvent(account, request.getLocale(), url));
            forceLogin(request, dto.getEmail(), dto.getPassword());
            log.info("redirecting to verification");
            ModelAndView mav = new ModelAndView("redirect:/verification", "account", new AccountDTO(dto.getFirstName(), dto.getLastName(), dto.getEmail(), dto.getCountry(), dto.getPassword(), dto.getConfirmPassword()));
            mav.addObject("cssPath", "../../../../css/index.css");
            return mav;
        } catch (UserExistsException eaeEx) {
            log.warn("User exists, redirecting back to signup page DTO: " + dto.getEmail());
            ModelAndView model = new ModelAndView("redirect:/signup", "account", new AccountDTO(dto.getFirstName(), dto.getLastName(), dto.getEmail(), dto.getCountry(), dto.getPassword(), dto.getConfirmPassword()));
            model.addObject("error", "This email already exists!");
            model.addObject("color", Color.ERROR.getColor());
            return model;
        }
}

日志显示它正确地传递了电子邮件,但是在/signup中它说它为空

4ngedf3f

4ngedf3f1#

我认为使用重定向属性更好

@PostMapping("register")
public ModelAndView register(@ModelAttribute("account") @Valid AccountDTO dto, RedirectAttributes attr, HttpServletRequest request) {
    log.info("Registering user");
    log.info("email: " + dto.getEmail());
        try {
            log.info("Checking if user exists");
            Account account = service.register(dto);
            String url = request.getContextPath();
            eventPublisher.publishEvent(new RegistrationCompleteEvent(account, request.getLocale(), url));
            forceLogin(request, dto.getEmail(), dto.getPassword());
            log.info("redirecting to verification");
            ModelAndView mav = new ModelAndView("redirect:/verification", "account", new AccountDTO(dto.getFirstName(), dto.getLastName(), dto.getEmail(), dto.getCountry(), dto.getPassword(), dto.getConfirmPassword()));
            mav.addObject("cssPath", "../../../../css/index.css");
            return mav;
        } catch (UserExistsException eaeEx) {
            log.warn("User exists, redirecting back to signup page DTO: " + dto.getEmail());
            ModelAndView model = new ModelAndView("redirect:/signup");
            attr.addFlashAttribute("account", new AccountDTO(dto.getFirstName(), dto.getLastName(), dto.getEmail(), dto.getCountry(), dto.getPassword(), dto.getConfirmPassword());
            attr.addFlashAttribute("error", "This email already exists!");
            attr.addFlashAttribute("color", Color.ERROR.getColor());
            return model;
        }
}

并接收帐户dto

@GetMapping("/signup")
@PreAuthorize("permitAll()")
public String signup(Model model, @RequestParam(value = "color", required = false) String color, @RequestParam(value = "error", required = false) String error, @ModelAttribute("account") AccountDTO dto) {
    log.info(dto.getEmail());
    if (dto == null) {
      model.addAttribute("account", new AccountDTO());
    } else {
      model.addAttribute("account", dto);
    }

    model.addAttribute("cssPath", "../css/index.css");
    model.addAttribute("title", "Welcome To Expense Tracker");
    model.addAttribute("error", error == null ?  "." : error);
    model.addAttribute("color", color == null ? Color.LIGHT_BACKGROUND.getColor() : color);
    return "signup/signup";
}

相关问题