在使用hibernate和jpa的spring mvc應用程序中,我在spring mvc應用程序中有一個表單,它需要將當前日期存儲在基礎數據中創建的行中的字段中表格提交時。我有模型,控制器和jsp都設置好了,但是當它運行時,我得到一個system.out.println()告訴我這個失敗是本地化的,不會生成創建日期。如何在下面更改我的代碼,以便創建日期生成併發送到需要的地方?在彈簧mvc應用程序中設置日期
它看起來可能是jquery datepicker函數在這段代碼中不起作用。但我不確定。一個新的觀點會有所幫助。
這是我的jsp:
<script>
$(function() {
$("#created").datepicker({ dateFormat: 'yy/mm/dd'});
});
</script>
<div class="container">
<jsp:include page="../fragments/bodyHeader.jsp"/>
<c:choose>
<c:when test="${document['new']}">
<c:set var="method" value="post"/>
</c:when>
<c:otherwise>
<c:set var="method" value="put"/>
</c:otherwise>
</c:choose>
<h2>
<c:if test="${document['new']}">New </c:if>
Document
</h2>
<form:form modelAttribute="document" method="${method}"
class="form-horizontal">
<div class="control-group" id="patient">
<label class="control-label">Patient </label>
<c:out value="${document.patient.firstName} ${document.patient.lastName}"/>
</div>
<myapp:inputField label="Name" name="name"/>
<myapp:inputField label="Description" name="description"/>
<div class="control-group">
<myapp:selectField name="type" label="Type " names="${types}" size="5"/>
</div>
<td><input type="file" name="file" id="file"></input></td>
<div class="form-actions">
<c:choose>
<c:when test="${document['new']}">
<button type="submit">Add Document</button>
</c:when>
<c:otherwise>
<button type="submit">Update Document</button>
</c:otherwise>
</c:choose>
</div>
</form:form>
<c:if test="${!document['new']}">
</c:if>
<jsp:include page="../fragments/footer.jsp"/>
</div>
</body>
這裏是控制器的相關部分:
@RequestMapping(value = "/patients/{patientId}/documents/new", method = RequestMethod.GET)
public String initCreationForm(@PathVariable("patientId") int patientId, Map<String, Object> model) {
Patient patient = this.clinicService.findPatientById(patientId);
Document document = new Document();
patient.addDocument(document);
model.put("document", document);
return "documents/createOrUpdateDocumentForm";
}
@RequestMapping(value = "/patients/{patientId}/documents/new", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("document") Document document, BindingResult result, SessionStatus status) {
new DocumentValidator().validate(document, result);
if (result.hasErrors()) {
//THIS IS BEING RETURNED BECAUSE DocumentValidator.validate() INDICATES THAT THERE IS NO created DATE
return "documents/createOrUpdateDocumentForm";
}
else {
this.clinicService.saveDocument(document);
status.setComplete();
return "redirect:/patients?patientID={patientId}";
}
}
這裏是DocumentValidator類,它是由上述控制裝置稱爲:
public class DocumentValidator {
public void validate(Document document, Errors errors) {
String name = document.getName();
// name validaation
if (!StringUtils.hasLength(name)) {
System.out.println("--------------------- No Name --------------------------");
errors.rejectValue("name", "required", "required");
}
else if (document.isNew() && document.getPatient().getDocument(name, true) != null) {
System.out.println("--------------------- Name Already Exists --------------------------");
errors.rejectValue("name", "duplicate", "already exists");
}
// type valication
if (document.isNew() && document.getType() == null) {
System.out.println("--------------------- No Type --------------------------");
errors.rejectValue("type", "required", "required");
}
// type valication
if (document.getCreated()==null) {
//THIS LINE IS BEING PRINTED BECAUSE created HAS NOT BEEN POPULATED WITH A VALUE
System.out.println("--------------------- No Created Date --------------------------");
errors.rejectValue("created", "required", "required");
}
}
}
這裏是模型的相關部分,這是Document.java的部分:
@Column(name = "created")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@DateTimeFormat(pattern = "yyyy/MM/dd")
private DateTime created;
public void setCreated(DateTime created) {this.created = created;}
public DateTime getCreated() {return this.created;}
上面的代碼編譯,但是當用戶按下輸入信息上傳一個文檔,月食後提交按鈕控制檯從DocumentValidator
中打印出指示日期字段created
不存在的行。
+1增加洞察力。我在相關問題上投入了大量獎金。你介意看相關的問題並提交答案嗎?我將獎勵那些提供解決方案的工作解決方案的人。這裏是鏈接:http://stackoverflow.com/questions/20586865/document-not-saving-in-spring-jpa-document-manager-application – CodeMed