【问题标题】:setting a date in spring mvc application在 spring mvc 应用程序中设置日期
【发布时间】:2013-12-28 17:46:10
【问题描述】:

在使用 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不存在。

【问题讨论】:

    标签: java spring hibernate jsp spring-mvc


    【解决方案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 用于增加洞察力。我在一个相关问题上投入了大量资金。您介意查看相关问题并提交答案吗?我会将巨额赏金奖励给提供有效解决方案并提供解释的人。这是链接:stackoverflow.com/questions/20586865/…
    【解决方案2】:

    您的表单中没有&lt;input name='created'&gt;

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2018-11-17
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2015-07-20
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多