title: Spring 3 MVC - Model not displaying in JSP Page
body: <p>On the surface, this question will sound a lot like questions that may have been asked previously on SOF but I tried those answers long ago and no luck. </p>
  
  <p>(For example, this question <a href="https://stackoverflow.com/questions/15335383/spring-3-not-rendering-model-in-jsp?rq=1">spring 3 not rendering model in jsp</a>)</p>
  
  <p>So I had a Spring 2.0 Controller that extended CancellableFormController, and it contained the following to bind the Command Object to the Form on JSP page render:</p>
  
  <pre><code>public class MyFormController extends CancellableFormController {
  
  ....
  
      protected Object formBackingObject(HttpServletRequest request) throws ServletException {
          MyCommand myCommand = new MyCommand();
              ... // add some list info to command object etc in preparation for page render
          return myCommand;
      }
  
  ....
  
  }
  </code></pre>
  
  <p>The JSP Page looked something like the following:</p>
  
  <pre><code>&lt;form:form method="post" commandName="myCommand"&gt;
  
      &lt;c:forEach items="${myCommand.myList}" var="myItem" varStatus="index"&gt;
  
  &lt;/form:form&gt;
  </code></pre>
  
  <p>To be clear, between the form tag definitions, a list of items was displayed on the JSP page. This is before any form submission has occurred (ie. at page render time). And what a beautifully simple world we lived in. Everything worked first time and there was peace on earth... sorry, enough sarcasm! ;)</p>
  
  <p>OK, so I happily tried to upgrade this old code to Spring 3 and annotations. My current problem is ONLY getting the Command object to render on page load. When I enter data into form fields and submit the form etc, the data gets bound correctly. What I am trying to do is pre-populate the Command object, and then render that content into the JSP page before the form is populated and submitted.</p>
  
  <p>I initially tried this:</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/myContextPath")
  public class MyFormController {
  
      ...
  
      @RequestMapping(method = RequestMethod.GET)
      public ModelAndView initForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
          ModelAndView mv = new ModelAndView("myTilesViewName");
          MyCommand myCommand = new MyCommand();
          ...
          mv.addObject("myCommand", myCommand);
          return mv;
      }
  
      ...
  
  }
  </code></pre>
  
  <p>Then I tried each of the following methods (the Controller class construct being the same as the above):</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.GET)
  @ModelAttribute("myCommand")
  public String initForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MyCommand myCommand = new MyCommand();
        ....
        return "myTilesViewName";
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String initForm(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception {
      MyCommand myCommand = new MyCommand();
      ...
      model.addAttribute("myCommand", cmd);
      return "myTilesViewName";
  }
  </code></pre>
  
  <p>I don't wish to add any confusion. I "imagine" that the answer to this problem is likely the same if I were just returning the view name of a JSP page... However, I am using Apache Tiles to handle the page selection. My definition looks something like:</p>
  
  <pre><code>&lt;definition name="myTilesViewName" extends=".myTemplate"&gt;
      &lt;put-attribute name="title" value="MyTitle" type="string" /&gt;
      &lt;put-attribute name="body" value="/WEB-INF/jsp/myPage.jsp" /&gt;
  &lt;/definition&gt;
  </code></pre>
  
  <p>Please remember everything worked perfectly before with the Spring 2 CancellableFormController code. I have not changed the Tiles config or JSP pages at all so don't believe they are the source of this problem.</p>
  
  <p>Thank you so much to anyone who can offer any assistance. It is certainly appreciated!</p>
  
  <h2>UPDATE:</h2>
  
  <p>This issue finally related to a Client side (JSP page) related problem. My assumption that "nothing had changed other than in the Controller" was incorrect. Sincere apologies to those who took the time to read this post and consider solutions. For anyone else reading with the same symptoms, please double check your View pages (JSP) and ensure they are syntactically correct for rendering. In my case, I had moved a JSP (with around 2000 lines of code) into a subfolder and broken a relative link to a nested jsp import declaration.</p>