gpt4 book ai didi

java - Spring MVC 表单提交 - 动态更改表单捕获对象

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:10 26 4
gpt4 key购买 nike

我的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,则表示字段“document”的对象将为MedicalCertificate.Java并且我将PatientDocument存储到MongoDB集合中。

MedicalCertificate.java 看起来像

public class MedicalCertificate {

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

}

客户端

我正在使用 Thymeleaf 进行客户端渲染

我的病人文档.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>

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

}

问题解释:如您所见,patentDocument.html 对应于字段“文档”的客户端表单内容将根据 documentType 字段被 Thymeleaf 片段替换。因此,当我将表单提交到 View Controller 时,对象 PatientDocument 包含 MedicalCertificate 字段来代替字段“文档”。没关系!!!

现在在 View Controller 中,我需要对提交处理程序说“嘿,PatientDocument.java 对象将以表单提交的形式出现。但是字段“document”内的对象将是“MedicalCertificate.java””

我可以在哪里指定?有没有办法在SPRING MVC中做到这一点?

在提交捕获客户端表单之前,我需要如下更改 patentDocument.java 对象。但如何呢?

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

提前致谢

美好的一天

最佳答案

选项 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"/>

您必须对其他表单进行类似的更改。然后,您可以根据 documentType 读取 PatientDocument 所需的属性。

<小时/>

选项2:为PatientDocument编写一个PropertyEditor来解析请求并根据请求参数设置文档。

关于java - Spring MVC 表单提交 - 动态更改表单捕获对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com