Я пытаюсь отобразить ошибки проверки на странице регистрации пользователя, построенной с помощью шаблона freemarker, если контроллер возвращает ошибки привязки.Ошибки проверки Spring MVC не отображаются в шаблоне Freemarker
код моего контроллера заключается в следующем:
@Controller
@RequestMapping("/")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
model.addAttribute("userForm", new User());
return "registration";
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("useraccount") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/explore";
}
в то время как это шаблон registration.ftl FreeMarker Я пытаюсь построить:
<div>
<fieldset>
<h1>Create your Account</h1>
<form id="regForm" class="idealform" action="registration" method="post" name='useraccount'>
Username: <input type="text" name="username" /> <errors path="username" cssClass="error"/><br/>
Password: <input type="text" name="password" /><errors path="password" cssClass="error"/><br/>
<label class="main-label" style="width: 91px;"> </label>
<input type="submit" value="submit">
</form>
</fieldset>
Я попытался также рекомендовал решение здесь:
Displaying Spring MVC validation errors in Freemarker templates
и registration.ftl становится:
<#assign form=JspTaglibs["http://www.springframework.org/tags/form"] />
<#macro formErrors>
<#assign formErrors><@form.errors path="*" /></#assign>
<#if formErrors?has_content>
<div id="errors">
<@spring.message "admin.error.globalMessage" />
</div>
</#if>
</#macro>
<div>
<fieldset>
<h1>Create your Account</h1>
<@form.form id="regForm" class="idealform" action="registration" method="post" name='useraccount'>
Username: <input type="text" name="username" path="username" /> <br/>
Password: <input type="text" name="password" path="password" /><br/>
<@formErrors />
<label class="main-label" style="width: 91px;"> </label>
<input type="submit" value="submit">
</@form.form>
</fieldset>
</div>
, но до сих пор сообщения проверки не отображаются.
Не могли бы вы поделиться своими мыслями со мной по этому вопросу? спасибо.