1

My Spring framework version 3.1.4

Question ??? Is there is any way to dynamically change the form catching Object depending on the some criteria.

Its really tough to explain, I will do my best here

JAVA OBJECTS

I have a Java Object PatientDocument.java

public class PatientDocument{

    @Id
    protected String documentId;

    @Indexed
    protected String patientId;

    @Indexed
    protected Integer documentType;

    protected Object document;

}

The field document in above class can have any datatype objects depending upon the value in field documentType Eg : If documentType is 1, the Object representing the field 'document' will be MedicalCertificate.Java and I'm storing the PatientDocument into MongoDB collection.

MedicalCertificate.java looks like

public class MedicalCertificate {

    protected String complaint;
    protected String suggestedRestingDays;
    protected Integer treatingDoctor;
    protected Integer medicalDirector;

}

CLIENT SIDE

I'm using Thymeleaf for my client side rendering

My patientDocument.html looks like

<form action="#" id="patientDocument" th:action="@{/emr/patient/document/save}" th:object="${patientDocument}" method="post" class="form-horizontal">

    <!-- #### HIDDEN FIELDS #### -->
    <input type="hidden" th:field="*{documentId}" class="col-xs-12" readonly="readonly"/>
    <input type="hidden" th:field="*{documentType}" class="col-xs-12" readonly="readonly"/>
    <input type="hidden" th:field="*{patientId}" class="col-xs-12" readonly="readonly"/>

    <!-- Medical Certificate -->
    <section th:if="${patientDocument.documentType == 1}" layout:include="@{emr/patient/medicalCertificate} :: main"></section>

    <!-- Referal Letter -->
    <section th:if="${patientDocument.documentType == 2}" layout:include="@{emr/patient/referalLetter} :: main"></section>

    <!-- Acknowledgment Form -->
    <section th:if="${patientDocument.documentType == 3}" layout:include="@{emr/patient/acknowledgeForm} :: main"></section>

<form>  

medicalCertificate.html looks like

<section layout:fragment="main">
    <div class="row">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="control-label col-xs-2">Complaint</label>
                <div class="col-xs-10">
                    <textarea rows="5" th:field="*{document.complaint}" class="col-xs-12"></textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">      
            <div class="form-group">
                <label class="control-label col-xs-4">Rest For</label>
                <div class="col-xs-8">
                    <input type="text" th:field="*{document.suggestedRestingDays}" class="col-xs-12"/>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <div class="form-group">
                <label class="control-label col-xs-4">Treated By</label>
                <div class="col-xs-8">
                    <input type="hidden" th:field="*{document.treatingDoctor}" readonly="readonly"/>
                    <input type="text" th:field="*{document.treatingDoctorName}" class="form-control"/>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="control-label col-xs-4">Medical Director</label>
                <div class="col-xs-8">
                    <input type="hidden" th:field="*{document.medicalDirector}" readonly="readonly"/>
                    <input type="text" th:field="*{document.medicalDirectorName}" class="form-control"/>
                </div>
            </div>
        </div>  
    </div>
</section>

View Controller

@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                   

}

Explanation of the Questions : As you can see the patientDocument.html 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 !!!

Now in View Controller, i need to say to the submit handler that "Hey, a PatientDocument.java object is coming as form submit. But the object inside the field 'document' will be 'MedicalCertificate.java'"

Where can i specify that?? Is there is any way to do it in SPRING MVC??

I need to alter the patientDocument.java Object as below before the Catching the client side form submit. But how?

PatientDocument patientDocument = new PatientDocument();
patientDocument.setDocument(new MedicalCertificate());

Thanks in advance

Good Day

2
  • I tried a lot to understand your problem, but its like hitting my head on hard rock. Could be you more specific to your problem? Commented Jul 31, 2014 at 9:56
  • @ShaileshSaxena Thanks for your time, As i mentioned, it was a bit tough to explain for me, sorry for the inconvenience. cheers !!! Commented Sep 14, 2014 at 12:09

1 Answer 1

1

Option 1: Change your container class to:

public class PatientDocument{
  @Id
  protected String documentId;

  @Indexed
  protected String patientId;

  @Indexed
  protected Integer documentType;

  protected AcknowledgeForm acknowledgeForm;

  protected MedicalCertificate medicalCertificate;

  protected ReferalLetter referalLetter;
}

Then, form fields in medicalCertificate.html would look like:

<textarea rows="5" th:field="*{medicalCertificate.complaint}" class="col-xs-12"></textarea>
<input type="text" th:field="*{medicalCertificate.suggestedRestingDays}" class="col-xs-12"/>
<input type="hidden" th:field="*{medicalCertificate.treatingDoctor}" readonly="readonly"/>
<input type="text" th:field="*{medicalCertificate.treatingDoctorName}" class="form-control"/>

You will have to make similar changes to the other forms. Then, you can read the required property of PatientDocument based on its documentType.


Option 2: Write a PropertyEditor for PatientDocument to parse the request and set the document based on the request parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply, I will leave option 1, since the document type will grow largly in future. option 2 sounds good. can you help me a bit more to write propertyEeditor in this case. Sorry for the Late response.
By ruling out option 1 you are looking for a PropertyEditor that gets an entire HttpServletRequest so that it can examine each and every form field. The PropertyEditor will then construct an object based on the information it finds in the submitted form. The trouble is that the PropertyEditor implementation you will provide will be a sub-class of java.beans.PropertyEditorSupport, which has no dependency on HttpServletRequest. So, there isn't an easy example of implementing option 2. You will have to go through the Spring MVC documentation to develop your own solution.
Well, That's make sense to me. Thanks once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.