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.
source share