title: Binding form parameters from JSP to spring controller
body: <p>I know this is a question which has been asked before. I did look at those questions but I was still not able to resolve my problem and thus writing in on stackoverflow. I am trying to bind the form parameters from my form addArticles.jsp to the controller. On the controller when I do a system.out.println I only get the categoryId and do not get the categoryName. I am not sure what I am doing is incorrect and pretty amused. </p>
  
  <p>addArticles.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&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;Add Article&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;center&gt;
          &lt;h2&gt;Create New Article&lt;/h2&gt;
          &lt;form:form action="/one2one/articles/save.do" method="POST" modelAttribute="command"&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="id"&gt;Article ID&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="id" value="${article.id}"&gt;&lt;/form:input&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="title"&gt;Article Title&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="title" value="${article.title}"&gt;&lt;/form:input&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="description"&gt;Article Description:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="description" value="${article.description}" cssStyle="width: 150px;"&gt;&lt;/form:input&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="category.categoryId"&gt;Category Type&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:select path="category.categoryId" cssStyle="width: 150px;"&gt;
                              &lt;option value="-1"&gt;Select a type&lt;/option&gt;
                              &lt;c:forEach items="${categories}" var="category"&gt;
                               &lt;option &lt;c:if test="${category.categoryName eq article.category.categoryName}"&gt;selected="selected"&lt;/c:if&gt;
                               value="${category.categoryId}"&gt;${category.categoryName}&lt;/option&gt;  
                               &lt;/c:forEach&gt; 
                          &lt;/form:select&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="keywords"&gt;Article Keywords:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="keywords" value="${article.keywords}"&gt;&lt;/form:input&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;&lt;form:label path="content"&gt;Article Content:&lt;/form:label&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;form:input path="content" value="${article.content}"&gt;&lt;/form:input&gt;&lt;/td&gt;
                  &lt;/tr&gt;
  
         &lt;tr&gt;
           &lt;td&gt;&amp;nbsp;&lt;/td&gt;
           &lt;td&gt;&lt;input type="submit" value="SAVE"/&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
              &lt;/table&gt;
          &lt;/form:form&gt;
  
              &lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ArticlesController.java</p>
  
  <pre><code>package com.java.bricks.controller;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  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.java.bricks.model.Article;
  import com.java.bricks.model.Category;
  import com.java.bricks.service.ArticleService;
  import com.java.bricks.service.CategoryService;
  
  @Controller("articlesController")
  @RequestMapping(value="/articles")
  public class ArticlesController {
  
      @Autowired
      private ArticleService articleService;
  
      @Autowired
      private CategoryService categoryService;
  
      @RequestMapping(value="/list", method=RequestMethod.GET)
      public ModelAndView listArticles(@ModelAttribute("command") Article article, BindingResult bindingResult){
  
          Map&lt;String,Object&gt; model = new HashMap&lt;String,Object&gt;();
          model.put("articles", articleService.listArticles());
          return new ModelAndView("articlesList",model);
      }
  
      @RequestMapping(value="/add",method=RequestMethod.GET)
      public ModelAndView addArticle(@ModelAttribute("command") Article article, BindingResult bindingResult) {
  
          Map&lt;String,Object&gt; model = new HashMap&lt;String,Object&gt;();
          model.put("articles", articleService.listArticles());
          model.put("categories", categoryService.listCategories());
          return new ModelAndView("addArticle",model);
      }
  
      @RequestMapping(value="/save",method=RequestMethod.POST)
      public ModelAndView saveArticle(@ModelAttribute("command") Article article, BindingResult bindingResult) {
  
          Map&lt;String,Object&gt; model = new HashMap&lt;String,Object&gt;();
          System.out.println("----------------------");
          System.out.println(article.getCategory());
          System.out.println(article.getCategory().getCategoryName());
          System.out.println(article.getCategory().getCategoryId());
          articleService.addArticle(article);
          model.put("articles",articleService.listArticles());
          return new ModelAndView("addArticle",model);
      }
  
  }
  </code></pre>
  
  <p>when I click save the first 4 lines of the SOP statements are </p>
  
  <p>System.out.println(article.getCategory());<br>
  output: Category [categoryId=29, categoryName=null]</p>
  
  <p>System.out.println(article.getCategory().getCategoryName());
  null</p>
  
  <p>System.out.println(article.getCategory().getCategoryId())
  29</p>
  
  <p>I am not sure why the categoryName is not populated in the controller. </p>
  
  <p>Article.java</p>
  
  <pre><code>package com.java.bricks.model;
  
  
  public class Article {
  
      private Long id;
      private String title;
      private String description;
      private String keywords;
      private String content;
      private Category category;
  
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
      public String getTitle() {
          return title;
      }
      public void setTitle(String title) {
          this.title = title;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
      public String getKeywords() {
          return keywords;
      }
      public void setKeywords(String keywords) {
          this.keywords = keywords;
      }
      public String getContent() {
          return content;
      }
      public void setContent(String content) {
          this.content = content;
      }
      public Category getCategory() {
          return category;
      }
      public void setCategory(Category category) {
          this.category = category;
      }
  
  
  }
  </code></pre>