title: Populating a drop-down from a database in spring
body: <p>I am having a jsp page containing a form for add ne item to a system. In there, I need to select the category that the new item belongs to. I need to populate this drop-down list from a database using spring. For the form submission, I am writing a controller class called ItemController.java where the drop-down element is which I need to populate. But, that populating list items need to be called from another controller class named CategoryController.java. I tried so many ways but still I could not able to do it as still getting errors. anybody can have a look on this please?  </p>
  
  <p>Here are my codes. </p>
  
  <p>addItem.jsp</p>
  
  <pre><code>  &lt;form:form class="form-horizontal" role="form" id="frmAddItem" action="/admin/items/add_item" method="post" commandName="command"&gt;
      &lt;fieldset class="scheduler-border"&gt;
  
      &lt;div class="form-group"&gt;
      &lt;div class="row"&gt;
      &lt;label for="selectCat" class="col-xs-3 control-label"&gt;
         Category
      &lt;/label&gt;
  
      &lt;div class="col-xs-5"&gt;
  
      &lt;form:select class="form-control" id="selectCat" path="categoryName"&gt;
      &lt;form:option value="-" label="--Select Category--"/&gt;
      &lt;form:options items="${listCat}" /&gt;
      &lt;/form:select&gt;
        &lt;span id="catErr" class="input-group-error"&gt;&lt;/span&gt;
      &lt;/div&gt;........
  </code></pre>
  
  <p>ItemController.java</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/items")
  public class ItemController {
  
      private static final Logger LOG = LogManager.getLogger(ItemController.class);
  
      @Autowired
      private ItemRepository item;
  
      @Qualifier("categoryRepository")
      @Autowired
      private CategoryRepository category;
  
      /**
       * Add new item view
       */
  
      //For viewing the add item form
      @RequestMapping(value = "/add", method = RequestMethod.GET)
      public ModelAndView showAddItem() {
          return new ModelAndView("addItem", "command", new Item());
      }
  
      //For submitting the add new item
      @RequestMapping(value = "/add_item")
      public String addItem(@ModelAttribute("newItem") Item newItem) throws SQLIntegrityConstraintViolationException {
  
      System.out.println("First Name:" + newItem.getItemName());
  
      int a = item.add(newItem);
      if (a == 1)
         return "redirect:add";
         // model.setViewName("addItem");
       else
  
       // System.out.println("Error in item add");
       return "redirect:add";
       //  return model;
      }
  </code></pre>
  
  <p>CategoryController.java</p>
  
  <pre><code>@Controller
  @RequestMapping("/items")
  public class CategoryController {
  
      private static final Logger LOG = LogManager.getLogger(CategoryController.class);
  
      @Autowired
      private CategoryRepository categoryRepository;
  
      @Autowired
      private ItemRepository item;
  
  
      @RequestMapping(value = "/add", method = RequestMethod.GET)
      public ModelAndView showAddItem() {
          ModelAndView model = new ModelAndView();
          List&lt;Map&lt;String, Object&gt;&gt; listCat = categoryRepository.viewCategoryList();
          model.addObject("listCat", listCat);
          return new ModelAndView("addItem", "command", new Item());
      }
  </code></pre>