title: Spring MVC Form Submit - Dynamically change the form catching Object
body: <p>My Spring framework version 3.1.4</p>
  
  <p><strong>Question ???</strong> Is there is any way to dynamically change the form catching Object depending on the some criteria. </p>
  
  <p>Its really tough to explain, I will do my best here</p>
  
  <h2>JAVA OBJECTS</h2>
  
  <p>I have a Java Object <code>PatientDocument.java</code></p>
  
  <pre><code>public class PatientDocument{
  
      @Id
      protected String documentId;
  
      @Indexed
      protected String patientId;
  
      @Indexed
      protected Integer documentType;
  
      protected Object document;
  
  }
  </code></pre>
  
  <p>The field <code>document</code> in above class can have any datatype objects depending upon the value in field <code>documentType</code> Eg : If documentType is 1, the Object representing the field 'document' will be <code>MedicalCertificate.Java</code> and I'm storing the PatientDocument into MongoDB collection.</p>
  
  <p><code>MedicalCertificate.java</code> looks like</p>
  
  <pre><code>public class MedicalCertificate {
  
      protected String complaint;
      protected String suggestedRestingDays;
      protected Integer treatingDoctor;
      protected Integer medicalDirector;
  
  }
  </code></pre>
  
  <h2>CLIENT SIDE</h2>
  
  <p>I'm using Thymeleaf for my client side rendering</p>
  
  <p>My patientDocument.html looks like</p>
  
  <pre><code>&lt;form action="#" id="patientDocument" th:action="@{/emr/patient/document/save}" th:object="${patientDocument}" method="post" class="form-horizontal"&gt;
  
      &lt;!-- #### HIDDEN FIELDS #### --&gt;
      &lt;input type="hidden" th:field="*{documentId}" class="col-xs-12" readonly="readonly"/&gt;
      &lt;input type="hidden" th:field="*{documentType}" class="col-xs-12" readonly="readonly"/&gt;
      &lt;input type="hidden" th:field="*{patientId}" class="col-xs-12" readonly="readonly"/&gt;
  
      &lt;!-- Medical Certificate --&gt;
      &lt;section th:if="${patientDocument.documentType == 1}" layout:include="@{emr/patient/medicalCertificate} :: main"&gt;&lt;/section&gt;
  
      &lt;!-- Referal Letter --&gt;
      &lt;section th:if="${patientDocument.documentType == 2}" layout:include="@{emr/patient/referalLetter} :: main"&gt;&lt;/section&gt;
  
      &lt;!-- Acknowledgment Form --&gt;
      &lt;section th:if="${patientDocument.documentType == 3}" layout:include="@{emr/patient/acknowledgeForm} :: main"&gt;&lt;/section&gt;
  
  &lt;form&gt;  
  </code></pre>
  
  <p>medicalCertificate.html looks like</p>
  
  <pre><code>&lt;section layout:fragment="main"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-12"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label class="control-label col-xs-2"&gt;Complaint&lt;/label&gt;
                  &lt;div class="col-xs-10"&gt;
                      &lt;textarea rows="5" th:field="*{document.complaint}" class="col-xs-12"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-6"&gt;      
              &lt;div class="form-group"&gt;
                  &lt;label class="control-label col-xs-4"&gt;Rest For&lt;/label&gt;
                  &lt;div class="col-xs-8"&gt;
                      &lt;input type="text" th:field="*{document.suggestedRestingDays}" class="col-xs-12"/&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label class="control-label col-xs-4"&gt;Treated By&lt;/label&gt;
                  &lt;div class="col-xs-8"&gt;
                      &lt;input type="hidden" th:field="*{document.treatingDoctor}" readonly="readonly"/&gt;
                      &lt;input type="text" th:field="*{document.treatingDoctorName}" class="form-control"/&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-xs-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label class="control-label col-xs-4"&gt;Medical Director&lt;/label&gt;
                  &lt;div class="col-xs-8"&gt;
                      &lt;input type="hidden" th:field="*{document.medicalDirector}" readonly="readonly"/&gt;
                      &lt;input type="text" th:field="*{document.medicalDirectorName}" class="form-control"/&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;  
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  
  <h2>View Controller</h2>
  
  <pre><code>@RequestMapping(value="/document/save", method=RequestMethod.POST)
  public String savePatientDocument(@ModelAttribute PatientDocument patientDocument, Model model, HttpServletRequest request){
  
          logger.debug("Executing save for Patient Document : {}", patientDocument.toString());
          ////Logic to the Service Layer                   
  
  }
  </code></pre>
  
  <p><strong>Explanation of the Questions</strong> :  As you can see the <code>patientDocument.html</code> the Client side form content corresponding to field 'document' will be replaced by Thymeleaf Fragments depending on documentType field. So when i submit the form into view controller the Object patientDocument contains the MedicalCertificate fields in place of field 'document'. Thats alright !!!</p>
  
  <p>Now in View Controller, i need to say to the submit handler that "Hey, a <code>PatientDocument.java</code> object is coming as form submit. But the object inside the field 'document' will be 'MedicalCertificate.java'"</p>
  
  <p>Where can i specify that?? Is there is any way to do it in SPRING MVC??</p>
  
  <p>I need to alter the <code>patientDocument.java</code> Object as below before the Catching the client side form submit. But how?</p>
  
  <pre><code>PatientDocument patientDocument = new PatientDocument();
  patientDocument.setDocument(new MedicalCertificate());
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p>Good Day</p>