我在Spring MVC中验证表单时没有显示错误消息

时间:2015-06-28 18:00:57

标签: java forms validation spring-mvc

我验证表单时遇到问题。表单会考虑验证,但不会显示错误消息。

控制器:

@RequestMapping(value = "/saveUser", method=RequestMethod.POST)
public String addUtilisateur(@Valid user util, BindingResult bindingResult, Model model)throws Exception{   
    if(bindingResult.hasErrors()){
        model.addAttribute("user", new user()); 
        return "login";
    }
        metier.ajoutUser(util);
    return "login";
}

User.java:

@Entity
public class user implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idUser;
@Column(unique=true)
@NotEmpty
private String login;
@NotEmpty
private String password;
private String action;
public String getLogin() {
    return login;
}
public void setLogin(String login) {
    this.login = login;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

public Long getIdUser() {
    return idUser;
}
public void setIdUser(Long idUser) {
    this.idUser = idUser;
}

public String getAction() {
    return action;
}
public void setAction(String action) {
    this.action = action;
}
public user() {
    super();
    // TODO Auto-generated constructor stub
}
public user(String login, String password) {
    super();
    this.login = login;
    this.password = password;
}

login.jsp:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:form modelAttribute="user" action="saveUser" method="post">
<table>
<tr>
<td>Login</td>
<td><f:input path="login"/>
<f:errors path="login" cssClass="errors"></f:errors> </td>
</tr>
<tr>
<td>Pass word</td>
<td><f:input type="password" path="password"/>
<f:errors path="password" cssClass="errors"></f:errors> </td>
</tr>
<tr>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</f:form>
</body>
</html>

有一个解决方案可以很好地显示错误信息吗?

0 个答案:

没有答案