Book Image

Spring MVC: Beginner's Guide - Second Edition

By : Amuthan Ganeshan
Book Image

Spring MVC: Beginner's Guide - Second Edition

By: Amuthan Ganeshan

Overview of this book

Spring MVC helps you build flexible and loosely coupled web applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also, Spring can integrate effortlessly with other popular web frameworks such as Struts, WebWork, Java Server Faces, and Tapestry. The book progressively teaches you to configure the Spring development environment, architecture, controllers, libraries, and more before moving on to developing a full web application. It begins with an introduction to the Spring development environment and architecture so you're familiar with the know-hows. From here, we move on to controllers, views, validations, Spring Tag libraries, and more. Finally, we integrate it all together to develop a web application. You'll also get to grips with testing applications for reliability.
Table of Contents (20 chapters)
Spring MVC Beginner's Guide - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Custom validation with JSR-303/Bean Validation


In the previous sections, we learned how to use standard JSR-303 Bean Validation annotations to validate fields of our domain object. This works great for simple validations, but sometimes we need to validate some custom rules that aren't available in the standard annotations. For example, what if we need to validate a newly added product's ID, which should not be the same as any existing product ID? To accomplish this type of thing, we can use custom validation annotations.

Time for action - adding Bean Validation support

In this exercise we are going to learn how to create custom validation annotations and how to use them. Let's add a custom product ID validation to our Add new product page to validate duplicate product IDs:.

  1. Create an annotation interface called ProductId (ProductId.java) under the package com.packt.webstore.validator in the source folder src/main/java, and add the following code snippet:

          package com.packt.webstore.validator...