【问题标题】:Spring MVC Form Submit - Dynamically change the form catching ObjectSpring MVC 表单提交 - 动态更改表单捕获对象
【发布时间】: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


【解决方案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"/>

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


方案二:为PatientDocument写一个PropertyEditor,解析请求,根据请求参数设置文档。

【讨论】:

  • 感谢您的回复,我将保留选项1,因为将来文档类型会大幅增长。选项 2 听起来不错。在这种情况下,你能帮我写propertyEeditor吗?抱歉回复晚了。
  • 通过排除选项 1,您正在寻找一个获得整个 HttpServletRequestPropertyEditor,以便它可以检查每个表单字段。然后PropertyEditor 将根据它在提交的表单中找到的信息构造一个对象。问题是您将提供的PropertyEditor 实现将是java.beans.PropertyEditorSupport 的子类,它不依赖于HttpServletRequest。因此,实现选项 2 的示例并不简单。您必须阅读 Spring MVC 文档来开发自己的解决方案。
  • 嗯,这对我来说很有意义。再次感谢
猜你喜欢
  • 2015-08-05
  • 2014-06-05
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多