I am building a simple Java-SpringMVC app and struggling with displaying error messages for invalid login/registration information. I continuously receive white label errors, as if the validation annotations are not working. For instance, I have the user model setup with @NotNull and @Size(min=4, max=128, message="example error message") for my username member variable; Even though this is the case if I purposely use a single character for the username, I still receive a white label error, the message I have listed with the annotation is even displayed via the white label error. However, I would like -- upon an error -- to be redirected to the same page and warned of the min/max character to the side of the input. form:error is also on my JSP as a place for the error to be shown.
Model
package com.kacygilbert.houseHunter.models;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min=4, max=128, message="username must be between 4 and 128 characters.")
private String username;
@NotNull
@Email(message="Invalid email address")
private String email;
@NotNull
@Size(min=8, max=128, message="Password must be at least 8 characters")
private String password;
@Transient // This is a field that doesn't go in the database. Doesn't need a column that represents it in the database table.
private String confirmPass;
@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
private List<Listing> listings;
@OneToMany(mappedBy="user", fetch=FetchType.LAZY)
private List<Note> notes;
@Column(updatable=false)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdAt;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date updatedAt;
public User() { }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPass() {
return confirmPass;
}
public void setConfirmPass(String confirm) {
this.confirmPass = confirm;
}
public List<Listing> getListings() {
return listings;
}
public void setListings(List<Listing> listings) {
this.listings = listings;
}
public List<Note> getNotes() {
return notes;
}
public void setNotes(List<Note> notes) {
this.notes = notes;
}
public Date getCreatedAt() {
return createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
@PrePersist
public void onCreate() {
this.createdAt = new Date();
if(updatedAt == null) {
updatedAt = new Date();
}
}
@PreUpdate
public void onUpdate() {
this.updatedAt = new Date();
}
}
VIEW
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- c:out ; c:forEach etc. -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!-- Formatting (dates) -->
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!-- form:form -->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!-- for rendering errors on PUT routes -->
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css"> <!-- change to match your file/naming structure -->
<title>House Hunter</title>
</head>
<body>
<h1>Welcome, House Hunter!</h1>
<div class="container">
<div class="register-form row">
<form:form action="/register" method="POST" modelAttribute="newUser" class="col-sm">
<h2 class="form-subtitle">Register</h2>
<div class="form-input">
<form:label class="form-label" path="username" >Username</form:label>
<form:input path="username" />
<form:errors path="username" />
</div>
<div class="form-input">
<form:label class="form-label" path="email"> Email </form:label>
<form:input path="email" />
<form:errors path="email" />
</div>
<div class="form-input">
<form:label class="form-label" path="password"> Password </form:label>
<form:input path="password" type="password" />
<form:errors path="password" />
</div>
<div class="form-input">
<form:label class="form-label" path="confirmPass"> Confirm Password </form:label>
<form:input path="confirmPass" type="password" />
<form:errors path="confirmPass" />
</div>
<button>Register!!</button>
</form:form>
</div>
<div class="login-form row">
<form:form action="/login" method="POST" modelAttribute="loginUser" class="col-sm">
<h2 class="form-subtitle">Log In Here</h2>
<div class="form-input">
<form:label class="form-label" path="email"> Email </form:label>
<form:input path="email" />
<form:errors path="email" />
</div>
<div class="form-input">
<form:label class="form-label" path="password"> Password </form:label>
<form:input path="password" type="password" />
<form:errors path="password" />
</div>
<button>Log In</button>
</form:form>
</div>
</div>
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Input the character 'z' in for username during "registration" and receive the following error:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Sep 20 02:51:37 CDT 2022
There was an unexpected error (type=Internal Server Error, status=500).
Validation failed for classes [com.kacygilbert.houseHunter.models.User] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='username must be between 4 and 128 characters.', propertyPath=username, rootBeanClass=class com.kacygilbert.houseHunter.models.User, messageTemplate='username must be between 4 and 128 characters.'} ]
javax.validation.ConstraintViolationException: Validation failed for classes [com.kacygilbert.houseHunter.models.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='username must be between 4 and 128 characters.', propertyPath=username, rootBeanClass=class com.kacygilbert.houseHunter.models.User, messageTemplate='username must be between 4 and 128 characters.'}
]
---removed for brevity---