Display form validation errors using Spring MVC

I searched for some guides and answers to this forum to try to solve my problem: I want to show validation errors from my bean in my form using spring MVC.

No matter what I try, I can not get it to work. I do not use redirects, my binding results directly after the model class, etc.

Here is what I still have:

Entrance Class:

public class LoginUser implements Serializable { @NotNull @Column(name="username", unique=true) @Size(min=5) private String username; @NotNull @Size(min=5) private String password; 

Input controller:

 @Transactional @Controller public class LoginController { @Autowired UserDao dao; @RequestMapping(value = "enter", method = RequestMethod.POST) public String doLogin(@ModelAttribute("user") @Valid LoginUser user, BindingResult result, HttpSession session) { if (result.hasErrors()) { return "login/loginForm"; } else { if (dao.authenticate(user)) { session.setAttribute("userLoggedIn", user.getUsername()); return "forward:index"; } else { return "redirect:login"; } } } 

Login form:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="resources/js/bootstrap.js"></script> <script type="text/javascript" src="resources/js/jquery-3.1.0.min.js"></script> <link rel="stylesheet" href="resources/css/bootstrap.css"> <title><spring:message code="title.login" /></title> </head> <body> <spring:message code="login.message.login" /> <form:form action="enter" commandName="loginForm" method="POST"> <spring:message code="username.login" /> <input type="text" name="username" id="username" /><br /> <form:errors path="username" /> <spring:message code="password.login" /> <input type="password" name="password" id="password" /><br /> <input type="submit" value="<spring:message code='button.login'/>"><br /> <spring:message code="does.not.have.account.login" /> <a href="register"><spring:message code="register.link.login" /></a> </form:form> </body> </html> 

Oh, I forgot to add - my messages.properties mood is set up (it works fine, checked), and messages are sent from there. Here is the line associated with the form:

messages_en.Properties

 NotEmpty.loginForm.username= Please fill the username field 
By the way, you can see that my view is mounted through a composite view (from JSP includes a header, a header and a footer), which overrides the usual load in Sping-MVC.
+1
source share
2 answers

You are using RedirectAttributes , but you are not redirecting the user anywhere. Try using "redirect:/<register_path>" or just add the Model model to the method parameters and use model.addAtribute("someName", result);

0
source

Try entering the code below.

 <form:form action="enter" commandName="loginForm" method="POST"> 

commandName loginForm , therefore @ModelAttribute ("loginForm").

 @RequestMapping(value = "enter", method = RequestMethod.POST) public String doLogin( @Valid @ModelAttribute("loginForm") LoginUser user, BindingResult result, Map<String, Object> model, HttpSession session) { if (result.hasErrors()) { return "login/loginForm"; } else { if (dao.authenticate(user)) { session.setAttribute("userLoggedIn", user.getUsername()); return "forward:index"; } else { return "redirect:login"; } } } 

UPDATE:

You are using NotEmpty.loginForm.username= Please fill the username field , which is incorrect , it must be NotNull.loginForm.username= Please fill the username field

And

@NotNull will not be checked for an empty string. The front end requirement will be "" , which is not null. Use @NotEmpty .

If you want to use @NotNull add InitBinder to your controller as shown below

 @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); } 

Then it will check for an empty string.

It is for this reason that you get size error directly, and since you did not mention a single message, the message is not displayed.

0
source

All Articles