A Spring MVC Contact Form

In our last post we laid the groundwork for Spring Email Services in a new Mail Boot app. We’ll be referencing that module in our Web application with a new User Contact Form. The form is on the NixMash Spring Demo site if you want to send me an email!

Here’s the form with a bad email “from” field, featuring the main issue we’ll be looking at in this post, efficient email form validation in Spring.

If you recall from our previous post on Spring Email we created a simple MailDTO container for our mail message. Here’s where the efficient validation comes in, because all we have to do to add email form validation is essentially hang Hibernate constraints on a few properties.

In our messages.properties file we’ll use Hibernate structured validation error messages, with the exception of ExtendedEmailValidator.email which we defined in our Custom ExtendedEmailValidator.

Spring MVC and Thymeleaf take care of the rest, here our Spring MVC method for Contact Form {POST}.

We’re using the Spring mailService component covered in the previous post that we built for our Mail Jar app. The Service does its thing and our MVC method sends a happy “Message Sent Successfully!” message back to our user.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.