【发布时间】:2021-06-13 02:48:41
【问题描述】:
我正在为我的网站创建一个注册页面,它可以正常提交。当电子邮件已经存在时,我希望它重新创建名字、姓氏、电子邮件和国家/地区的值。问题是它可以很好地显示文本,但是不会重新创建值。
我的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 中它说它为空
【问题讨论】:
-
从
register()方法中,dto可能需要添加到返回的ModelAndView中,以便"*{firstName}"可以发现属性。
标签: java spring spring-mvc spring-security