title: Use Spring 3 MVC Annotation Validation with Freemarker
body: <p>I am using Spring 3 MVC Annotation Validation. I know how to do it in JSP(using <code>modelAttribute</code> property in JSP), but I don't have any clue how to apply this kind of data binding in Freemarker.</p>
  
  <p>Example of JSP equivalence</p>
  
  <pre><code>&lt;c:url var="addUrl" value="/admin/workerAdd" /&gt;
  &lt;form:form modelAttribute="worker" action="${addUrl}" method="post"&gt;
      &lt;p&gt;
   &lt;form:label for="code" path="code"&gt;Code&lt;/form:label&gt;
       &lt;form:input path="code" readonly="false" /&gt;
      &lt;/p&gt;
  ...
  </code></pre>
  
  <p>Example of Controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/admin/*")
  public class AdminController {    
      @Autowired
      private CommonService commonService;
  
      /**
       * For every request for this controller, this will 
       * create a person instance for the form.
       */
      @ModelAttribute
      public Worker newRequest(@RequestParam(required=false) String id) {
          return (id != null ? commonService.getWorkerById(id) : new Worker());
      }
  
      @RequestMapping(value="/workerAdd", method=RequestMethod.POST)
      public final String performAddUser(@Valid Worker worker, BindingResult result) throws Exception {
          if (result.hasErrors()) {
              return null;
          }
  
              // Create the worker here.
  
          return "redirect:/administration/manageUser";
      }
  </code></pre>
  
  <p>Now I want to use the same controller, but the views are written by Freemarker(ftl). The data-binding in my below freemarker doesn't work (which is understandable, since it should have different syntax). I did some research and learn about command object in FTL, but I don't get the point. I think it should be a similar attribute that tell Spring to do the binding but I still not find it yet.</p>
  
  <pre><code>&lt;form id="worker" modelAttribute="worker" action="${rc.getContextUrl('/admin/workerAdd')}" method="post" &gt;          
              &lt;div class="Entry"&gt;
              &lt;label for="code"&gt;Code&lt;/label&gt;
              &lt;input type="text" id="code" name="code" value="${worker.code}" /&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>Is there any simple way to make this annotation validation way (and the databinding as well) works with FTL? Any help will be appreciated.</p>
  
  <p>Thanks,</p>
  
  <p>Hoang Long</p>