【发布时间】:2014-07-31 09:06:23
【问题描述】:
我的 Spring 框架版本 3.1.4
问题??? 有什么方法可以根据某些条件动态更改捕获对象的表单。
真的很难解释,我会尽力做到的
Java 对象
我有一个 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,则表示字段“文档”的对象将为MedicalCertificate.Java,我正在存储将 PatientDocument 放入 MongoDB 集合中。
MedicalCertificate.java 看起来像
public class MedicalCertificate {
protected String complaint;
protected String suggestedRestingDays;
protected Integer treatingDoctor;
protected Integer medicalDirector;
}
客户端
我正在使用 Thymeleaf 进行客户端渲染
我的 patientDocument.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>
medicalCertificate.html 看起来像
<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
}
问题说明:如您所见,patientDocument.html 字段“document”对应的客户端表单内容将根据 documentType 字段替换为 Thymeleaf 片段。因此,当我将表单提交到视图控制器时,Object patientDocument 包含 MedicalCertificate 字段代替字段“文档”。没关系!!!
现在在 View Controller 中,我需要对提交处理程序说“嘿,PatientDocument.java 对象将作为表单提交。但是字段 'document' 内的对象将是 'MedicalCertificate.java'”
我在哪里可以指定?在 SPRING MVC 中有什么方法可以做到吗??
在捕获客户端表单提交之前,我需要如下更改patientDocument.java 对象。但是怎么做呢?
PatientDocument patientDocument = new PatientDocument();
patientDocument.setDocument(new MedicalCertificate());
提前致谢
美好的一天
【问题讨论】:
-
我尝试了很多来理解您的问题,但这就像在坚硬的岩石上敲我的头。您能否更具体地解决您的问题?
-
@ShaileshSaxena 感谢您抽出宝贵时间,正如我所提到的,向我解释有点困难,很抱歉给您带来不便。干杯!!!
标签: java spring forms spring-mvc thymeleaf