title: Java form validation
body: <p>I have a website which has a search box. I want to validate the search box when a user clicks on <em>search</em> without entering any text. He should get an error message, but I am getting the following error:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'searchForm' available as request attribute</p>
  </blockquote>
  
  <p>My form is as follows:</p>
  
  <pre><code>&lt;blc:form th:action="@{/search}" th:object="${searchForm}"  method="POST"&gt;
      &lt;input type="search" class="search" name="q" th:value="${originalQuery}" /&gt;
          &lt;span th:if="${#fields.hasErrors('searchText')}" th:errors="*{searchText}"&gt;dddd&lt;/span&gt;
      &lt;input type="submit" id="search_button" value="go" /&gt;
  &lt;/blc:form&gt;
  </code></pre>
  
  <p>My controller is as follows:</p>
  
  <pre><code>@Controller
  @RequestMapping("/search")
  public class SearchController extends Mysearchcontroller {
      @RequestMapping("")
      public String search(HttpServletRequest request, HttpServletResponse response,
              @RequestParam(required = false) String q,@ModelAttribute("mysearchForm") Mysearchform mysearchForm,BindingResult errors,Model model) throws ServletException, IOException, ServiceException {
          return search3(model, request,response, q ,errors);
      }
  }
  </code></pre>