我正在使用 Spring Boot 2 和 Thymeleaf 创建一个MVC应用程序。
在为复杂对象创建表单时遇到了一些麻烦。我得到了MedicalJournalEntry类,该类具有一些原始属性,还具有BloodPressure类(关联)的嵌套对象。在这里您可以看到两个类:
MedicalJournalEntry类:
@Entity
@Table(name="MED_JOURNAL_ENTRY")
public class MedicalJournalEntry extends JournalEntry{
/*
* The following attributes are inherited from the superclass JournalEntry
* intJournalEntryId
* datJournalEntryDate
* tstmpJournalEntryCreateDate
* tstmpJournalEntryUpdateDate
*
* getters and setters are defined and implemented in the superclass JournalEntry as well
*
*/
@Embedded
private BloodPressure bloodPressure;
@ManyToOne
@JoinColumn(name = "JOURNAL_ID")
@JsonBackReference
private MedicalJournal medJournal; // bidirectional relationship with entity MedicalJournal
@Column(name="JOURNAL_ENTRY_PULSE")
private short shPulse;
@Column(name="JOURNAL_ENTRY_HEIGHT")
@Min(value = 1, message="Die Größe muss größer als 1cm sein.")
@Max(value = 300, message="Die Größe muss kleiner als 300cm sein.")
private short shHeight; // in centimeters
@Column(name="JOURNAL_ENTRY_WEIGHT")
@Min(value = 1, message="Das Gewicht muss größer als 1kg sein.")
@Max(value = 500, message="Das Gewicht muss kleiner als 500kg sein.")
private short shWeight; // in kilograms
@Column(name="JOURNAL_ENTRY_BMI")
private float fltBmi;
@Column(name="JOURNAL_ENTRY_IS_SICK")
private boolean blnIsSick;
// constructors and methods ...
}
血压水平:
@Embeddable // is part of MedicalJournalEntry
public class BloodPressure {
public BloodPressure(int intSYS, int intDIA) {
this.intSYS = intSYS;
this.intDIA = intDIA;
}
@Column(name ="BLOOD_PRESSURE_SYS")
private int intSYS;
@Column(name = "BLOOD_PRESSURE_DIA")
private int intDIA;
// constructors and mehtods ...
}
我已经使用片段创建了表单。它对于“常规”字段确实非常有效,但是由于我添加了嵌套对象字段,因此Thymeleaf甚至无法创建保存该表单的视图。当我想显示表单时,总是出现以下错误:
处理器执行期间发生错误
org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:“ create-medJournalEntry”
我的百里香叶形式如下:
<form action = "#" th:action="@{/medJourEntry/add}" th:object=${medicalJournalEntry} method ="post">
<div th:replace="edit-medJournalEntry.html :: medEntryForm(medEntry=${#object})"></div>
<button type="submit" class="btn btn-primary">Tagebucheintrag erstellen</button>
<button type="reset" class="btn btn-primary">Felder zurücksetzen</button>
</form>
以及导致问题的片段的相应内容:
</div>
<div class ="form-row">
<div class="form-group col-md-4">
<label for="bloodpressureSys">Blutdruck systolisch</label>
<input type="text" th:field="*{bloodPressure.intSys}" class="form-control" id="bloodpressureSys" aria-describedby="bloodpressureSystHelp" placeholder="Systolischer Blutdruck">
<div th:if="${#fields.hasErrors('${medicalJournalEntry.bloodPressure.intSys}')}">
<div class="alert alert-danger" role="alert">
<p th:each="err : ${#fields.errors('bloodPressure.intSys')}" th:text="${err}"></p>
</div>
</div>
<small id="bloodpressureSystHelp" class="form-text text-muted">Bitte tragen Sie den systolischen Blutdruck (in mmHg) ein.</small>
</div>
<div class="form-group col-md-4">
<label for="bloodpressureDia">Blutdruck Diastolisch</label>
<input type = "text" th:field="*{bloodPressure.intDia}" class="form-control" id="bloodpressureDia" aria-describedby="bloodpressureDiaHelp" placeholder="Diastolischer Blutdruck">
<small id="bloodpressureDiaHelp" class="form-text text-muted">Bitte tragen Sie den diastolischen Blutdruck (in mmHg) ein.</small>
</div>
</div>
这是我的控制器。它仅返回包含新MedicalJournalEntry的输入数据形式的视图:
@RequestMapping(value="/create", method = RequestMethod.GET)
public String showCreateMedicalJournalEntryForm(MedicalJournalEntry medicalJournalEntry) {
return "create-medJournalEntry";
}
答案 0 :(得分:0)
感谢所有尝试帮助我的人!
我们刚刚解决了我们的问题。
MedicalJournalEntry类的no-arg构造函数缺少空的BloodPressure对象的构造。这是强制性的,因为否则基本上会得到某种NullPointerException,因为子对象只是不活跃。
因此,此问题已解决。再次感谢大家,非常感谢! :)