title: How exactly works the supports() method of the Spring Validator interface?
body: <p>I am pretty new in Spring MVC and I have the following doubt about how works this validator example:</p>
  
  <p>So I have this <strong>search.jsp</strong> page in which the user can search a product by 2 fields criteria (the product name and the category to which the product belongs):</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Search&lt;/title&gt;
  &lt;jsp:include page="/WEB-INF/views/include/head-include.jsp" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class="container"&gt;
      &lt;h2&gt;Search Products&lt;/h2&gt;
      &lt;div class="form-group form"&gt;
      &lt;form:form name="input"  method="get"
          modelAttribute="product" action="products"&gt;
          &lt;div&gt;
              &lt;label&gt;Name: &lt;/label&gt;
              &lt;form:input type="text" path="name" class="form-control"  /&gt;
              &lt;form:errors path="name" /&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label&gt;Category:&lt;/label&gt;
              &lt;form:select path="category.id" class="form-control" &gt;
                  &lt;form:option path="category.id" value="0" label="--- All Categories ---"/&gt;
                  &lt;form:options path="category.id" items="${categories}" itemLabel="name" itemValue="id" /&gt;
              &lt;/form:select&gt;
          &lt;/div&gt;
          &lt;div&gt;
              &lt;input type="submit" value="Submit" class="form-control"&gt;
          &lt;/div&gt;
  
  
      &lt;/form:form&gt;
      &lt;/div&gt;
  
      &lt;h2&gt;Search Results:&lt;/h2&gt;
  
      &lt;c:forEach var="product" items="${results}"&gt;
          &lt;c:out value="${product.name}"/&gt; &lt;br/&gt;
      &lt;/c:forEach&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, in the tutorial are introduced some restriction on the input of the 2 fields of the previous view implementing a <strong>custom validator</strong>. In the specific the user have to:</p>
  
  <p>1) The user have to insert a product name or a category</p>
  
  <p>2) The name lenght have to be at least composed by 3 characters.</p>
  
  <p>3) The user can chose only by name or category but not on some other field of the <strong>Product</strong> model object</p>
  
  <p>To implement this custom validator it is created this <strong>ProductSearchValidator</strong> class that implement the Spring <strong>Validator</strong> interface:</p>
  
  <pre><code>package com.packtpub.springmvc.chocolatestore;
  
  import org.springframework.stereotype.Component;
  import org.springframework.util.StringUtils;
  import org.springframework.validation.Errors;
  import org.springframework.validation.Validator;
  
  import com.packtpub.springmvc.chocolatestore.model.Product;
  
  @Component
  public class ProductSearchValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          return Product.class.isAssignableFrom(clazz);  
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          Product product = (Product) target;
          String name = product.getName();
          if (!StringUtils.hasLength(name) &amp;&amp; (product.getCategory() == null || product.getCategory().getId() == 0)) {
              errors.rejectValue("name", "required", "Either name or category is required");
          } else if ((product.getCategory() == null || product.getCategory().getId() == 0) &amp;&amp; name.trim().length() &lt; 3) {
              errors.rejectValue("name", "tooShort", "Please enter at least 3 letters");
          }
  
      }
  
  }
  </code></pre>
  
  <p>From what I have understand the <strong>supports()</strong> method say if a specific instance of a class can be validate from this validator and the <strong>validate()</strong> method implement the specific validation logic.</p>
  
  <p>Ok, the <strong>validate()</strong> method is pretty clear to me (it implement the previous 3 specified restriction).</p>
  
  <p>The thing that I can't understand is how exactly does the <strong>supports()</strong> method</p>
  
  <pre><code>@Override
  public boolean supports(Class&lt;?&gt; clazz) {
      return Product.class.isAssignableFrom(clazz);  
  }
  </code></pre>
  
  <p>How exactly works? What represent its input parameter (<strong>Class clazz</strong>)? And what represent the returned boolean? (what is the meaning of the <strong>Product.class.isAssignableFrom(clazz)</strong> evaluation?)</p>
  
  <p>Tnx</p>