Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Spring MVC Beginner's Guide

You're reading from   Spring MVC Beginner's Guide Your ultimate guide to building a complete web application using all the capabilities of Spring MVC

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783284870
Length 304 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Amuthan Ganeshan Amuthan Ganeshan
Author Profile Icon Amuthan Ganeshan
Amuthan Ganeshan
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Configuring a Spring Development Environment 2. Spring MVC Architecture – Architecting Your Web Store FREE CHAPTER 3. Control Your Store with Controllers 4. Working with Spring Tag Libraries 5. Working with View Resolver 6. Intercept Your Store with Interceptor 7. Validate Your Products with a Validator 8. Give REST to Your Application with Ajax 9. Apache Tiles and Spring Web Flow in Action 10. Testing Your Application A. Using the Gradle Build Tool B. Pop Quiz Answers Index

Time for action – adding Spring validation


If you have a constraint that doesn't allow anyone to add more than 99 units of any product if the unit price is greater than 1000 USD for that product, add such a validation using Spring validation in the project. Perform the following steps:

  1. Create a class called UnitsInStockValidator under the com.packt.webstore.validator package in the source folder src/main/java. Add the following code into it:

    package com.packt.webstore.validator;
    
    import java.math.BigDecimal;
    import org.springframework.stereotype.Component;
    import org.springframework.validation.Errors;
    import org.springframework.validation.Validator;
    import com.packt.webstore.domain.Product;
    
    @Component
    public class UnitsInStockValidator implements Validator{
    
      public boolean supports(Class<?> clazz) {
        return Product.class.isAssignableFrom(clazz);  
      }
    
      public void validate(Object target, Errors errors) {
        Product product = (Product) target;
    
        if(product.getUnitPrice()!...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime