title: Separate model and UI view model in Spring (sort of MVVM in Spring)
body: <p>I would like to start a discussion on separating pure model and UI model in Spring 3.</p>
  
  <p>By pure model I mean main object/objects that I retrieve from database, let's say some "user account". It contains enough info to display it in HTML view or to pass it to web service.</p>
  
  <p>By UI model I mean all the auxillary stuff I need in UI to work with that object. E.g. if a "user account" has "state", then I need to fetch all the "states" from the database for, say, a combo box. The views are tricky and in some cases they require more information, in others - less. It would also be nice to be able to alter some lists by adding items like "Select all", which is pure UI stuff (and not quite conveniently done from view template).</p>
  
  <p>I heard there's so called Model-View-ViewModel pattern, which seems to address these issues, but I have never tried its implementations.</p>
  
  <p>The solution I use right now is to break logics into two services - one for pure model and one for UI model. It looks like this:</p>
  
  <pre><code>@RequestMapping(value="app/user_accounts/{id}")
  public String getUserAccount(@PathVariable("id") String id) {
      service.getUserAccount(id); // Gets main object and puts it into model
      presenter.formUserAccount(); // Gets all classifier for main object's properties
      return "user_account";
  }
  </code></pre>
  
  <p>What I dislike about this is that the view and its so-called view model are not attached to each other. I can call presenter.formUserAccount() and return totally unrelated view name. </p>
  
  <p>Another approach I see is similar to Spring controller annotation. We have classes annotated as @ViewModels and methods which map to view names. An interceptor finds and executes these methods before rendering a certain view. These seems elegant enough, but requires much code writing.</p>
  
  <p>How would you solve this problem?</p>