在使用hibernate和jpa的spring mvc应用程序中,我在spring mvc应用程序中有一个表单,需要将当前日期存储在提交表单时在基础数据表中创建的行中的字段中。我已经设置了模型,控制器和jsp,但是当它运行时,我得到一个system.out.println()告诉我故障已经本地化到没有生成的创建日期。如何更改下面的代码,以便生成创建日期并发送到需要的位置?
看起来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;}
上面的代码编译,但是当用户在输入上传文档的信息后按下提交按钮时,eclipse控制台会打印出来自DocumentValidator
的行,表示日期字段created
没有存在。
答案 0 :(得分:1)
将datePicker控件绑定到代码中的输入类型元素
$("#created").datepicker({ dateFormat: 'yy/mm/dd'});
根据这个你将datepicker控件绑定到具有Id =“created”属性的元素,我没有在jsp文件中看到任何元素具有Id =“created”
您必须将datePicker控件映射到某个输入元素
<form:input path="created" id="created" class="date-pick" readonly="true" />
希望这能解决您的问题
答案 1 :(得分:0)
表单中没有<input name='created'>