我的Spring框架版本3.1.4
问题??? 是否有任何方法可以根据某些条件动态更改表单捕获对象。
很难解释,我会尽力而为
我有一个Java对象PatientDocument.java
public class PatientDocument{
@Id
protected String documentId;
@Indexed
protected String patientId;
@Indexed
protected Integer documentType;
protected Object document;
}
上面的类中的字段document
可以具有任何数据类型对象,具体取决于字段documentType
中的值。例如:如果documentType为1,则表示字段“document”的Object将为{{1}我将PatientDocument存储到MongoDB集合中。
MedicalCertificate.Java
看起来像
MedicalCertificate.java
我正在使用Thymeleaf进行客户端渲染
我的patientDocument.html看起来像
public class MedicalCertificate {
protected String complaint;
protected String suggestedRestingDays;
protected Integer treatingDoctor;
protected Integer medicalDirector;
}
medicalCertificate.html看起来像
<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>
<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>
问题说明:正如您所见,@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
}
对应于字段'document'的客户端表单内容将被Thymeleaf Fragments替换,具体取决于documentType字段。因此,当我将表单提交到视图控制器时,Object patientDocument包含MedicalCertificate字段来代替字段'document'。多数民众赞成!
现在在View Controller中,我需要向提交处理程序说“嘿,一个patientDocument.html
对象即将作为表单提交。但是字段'document'中的对象将是'MedicalCertificate.java'”
我在哪里可以指定?在SPRING MVC中有没有办法做到这一点?
在捕获客户端表单提交之前,我需要更改PatientDocument.java
对象,如下所示。但是如何?
patientDocument.java
提前致谢
美好的一天
答案 0 :(得分:1)
选项1 :将容器类更改为:
public class PatientDocument{
@Id
protected String documentId;
@Indexed
protected String patientId;
@Indexed
protected Integer documentType;
protected AcknowledgeForm acknowledgeForm;
protected MedicalCertificate medicalCertificate;
protected ReferalLetter referalLetter;
}
然后,medicalCertificate.html
中的表单字段将如下所示:
<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"/>
您必须对其他表单进行类似的更改。然后,您可以根据PatientDocument
。
documentType
所需的属性
选项2 :为PropertyEditor
写一个PatientDocument
来解析请求,并根据请求参数设置文档。