title: Spring MVC -- Handle multiple forms in a single jsp
body: <p>I have been searching around for reason why I have error <code>Neither BindingResult nor plain target object for bean name 'addItemForm' available as request attribute</code> but can't understand. </p>
  
  <p>The page is loading fine but if I click on <code>Add new item</code> button, a lightbox containing a form appears, I click on <code>Add item</code> and I got same error as above but bean name is <code>searchForm</code>.</p>
  
  <p>The controller looks fine, I think there is a binding problem between the view and the form. Can someone explain please?</p>
  
  <p>Also, it is able to add new items to database, so the POST method is working, but the view returns errors ..</p>
  
  <p>JSP: </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 prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&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;Admin View&lt;/title&gt;
      &lt;style type="text/css"&gt;
          body{margin:0, padding:0}
          #body{
              text-align: center;
              top: 3%;
          }           
  
          .table2{
              border-spacing: 5%;
          }
  
          .table1 {
              border-collapse: collapse;          
              width: 80%;
          }
  
          .table1 tr:nth-child(odd) td{
              background-color: #ffffff;
          }
          .table1 tr:nth-child(even) td{
              background-color: #4da6ff;
          }   
  
          .table1 td, th {
              text-align: left;
              border: 1px solid green;
          }
  
          .black_overlay{
              display: none;
              position: absolute;
              top: 0%;
              left: 0%;
              width: 100%;
              height: 100%;
              background-color: black;
              z-index:1001;
              -moz-opacity: 0.8;
              opacity:.80;
              filter: alpha(opacity=80);
          }
          .white_content {
              display: none;
              position: absolute;
              top: 25%;
              left: 25%;
              width: 50%;
              height: 50%;
              padding: 16px;
              border: 16px solid orange;
              background-color: white;
              z-index:1002;
              overflow: auto;
          }           
      &lt;/style&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="body"&gt;
          &lt;h3&gt;This is the Administrator View&lt;/h3&gt;
          &lt;p&gt;From here, you can search for items in the database, view, add new items and delete items from database&lt;/p&gt;
  
          &lt;table align="center"&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;form:form action="AdminView" method="POST" commandName="searchForm"&gt;
                          &lt;form:input path="KeyWordSearch" size="40"/&gt;
                          &lt;input type="submit" name="search" value="Search Store"/&gt;                                       
                      &lt;/form:form&gt;
                  &lt;/td&gt;
                  &lt;td&gt;&lt;button onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"&gt;Add new item&lt;/button&gt;&lt;/td&gt;             
              &lt;/tr&gt;           
          &lt;/table&gt;                
          &lt;br&gt;
          &lt;c:if test="${not empty itemList}"&gt;
              &lt;table class="table1" align="center"&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Item Name&lt;/th&gt;
                      &lt;th&gt;Date added&lt;/th&gt;                 
                      &lt;th&gt;Item Description&lt;/th&gt;
                      &lt;th&gt;View Details&lt;/th&gt;
                  &lt;/tr&gt;
  
                  &lt;c:forEach var="item" items="${itemList}"&gt;
                      &lt;tr&gt;
                          &lt;td&gt;${item.itemName}&lt;/td&gt;
                          &lt;td&gt;${item.itemAdded}&lt;/td&gt;
                          &lt;td&gt;${item.itemDescription}&lt;/td&gt;
                          &lt;td&gt;&lt;a href="/myapp/DisplayItemDetailsForAdmin?itemId=${item.itemId}"&gt;View&lt;/a&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/c:forEach&gt;                
              &lt;/table&gt;
          &lt;/c:if&gt;
      &lt;/div&gt;  
  
      &lt;div id="light" class="white_content"&gt;
          &lt;h2 align="center"&gt;Add new item to Store&lt;/h2&gt;
          &lt;form:form action="AdminView" method="POST" commandName="addItemForm"&gt;
              &lt;table align="left"&gt;
                  &lt;tr&gt;
                      &lt;th style="border:0"&gt;Item name&lt;/th&gt;
                      &lt;td&gt;&lt;form:input path="ItemName"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;th style="border:0"&gt;Item location&lt;/th&gt;
                      &lt;td&gt;&lt;form:input path="ItemLocation"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;th style="border:0"&gt;Item Description&lt;/th&gt;
                      &lt;td&gt;&lt;form:textarea path="ItemDescription"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;input type="submit" name="add" value="Add Item"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/form:form&gt; 
          &lt;a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"&gt;Close&lt;/a&gt;
      &lt;/div&gt;
      &lt;div id="fade" class="black_overlay"&gt;&lt;/div&gt;     
  &lt;/body&gt;
  &lt;/html
  </code></pre>
  
  <p>This is the Controller:</p>
  
  <pre><code>package com.test.controller;
  
  import java.util.List;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.test.forms.AddNewItemForm;
  import com.test.forms.SearchForm;
  import com.test.models.items.ItemIF;
  import com.test.transaction.TransactionFactory;
  
  /**
   * Handles requests for Admin View page
   * @author Trung
   *
   */
  @Controller
  @RequestMapping (value = "/AdminView")
  public class AdminViewController {
  
      @RequestMapping(method = RequestMethod.GET)
      public ModelAndView adminForm(Model model){
          SearchForm searchForm = new SearchForm();
          model.addAttribute("searchForm", searchForm);
  
          AddNewItemForm addItemForm = new AddNewItemForm();
          model.addAttribute("addItemForm", addItemForm);
  
          return new ModelAndView("AdminView");
      }
  
      @RequestMapping (params = "search", method = RequestMethod.POST)
      public String processingSearchStore(@ModelAttribute("searchForm") SearchForm searchForm, Model model){
          List&lt;ItemIF&gt; relatedItems = null;
          TransactionFactory transaction = new TransactionFactory();
          relatedItems = transaction.retrieveItemByName(searchForm.getKeyWordSearch());
  
          if (relatedItems.isEmpty()){
              System.out.println("okay, there isn't any item that is matched your criteria");
          } else {
              model.addAttribute("itemList", relatedItems);
          }
  
          return "AdminView";
      }
  
      @RequestMapping(params = "add", method = RequestMethod.POST)
      public ModelAndView addNewItemForm(@ModelAttribute("addItemForm") AddNewItemForm addItemForm){
          TransactionFactory transaction = new TransactionFactory();
          String itemName = addItemForm.getItemName();
          String itemLocation = addItemForm.getItemLocation();
          String itemDescription = addItemForm.getItemDescription();
  
          transaction.insertItem(itemName, itemLocation, itemDescription);
  
          return new ModelAndView("AdminView");
      }
  }
  </code></pre>