簡體   English   中英

提交表單時,帶有日期/時間的 Spring 為空

[英]Spring filed with date/time is null when the form is submited

我的控制器中有兩個映射。 這是一個 GET 映射:

@RequestMapping(value="/items/book-list/edit", method = RequestMethod.GET)
public String showEditBookPage(@RequestParam Long id, ModelMap model){
    Book book = bookService.findBookById(id);
    model.addAttribute("editForm", book);
    LOG.info("Logged modified date once page is loaded: " + book.getModifyDate());
    return "admin/book";
}

此映射僅用於顯示小表單的 book.jsp 頁面。

我還有另一個相同值的映射,但使用 POST 方法,用於提交表單。

 @RequestMapping(value="/items/book-list/edit", method = RequestMethod.POST)
public String updateBook(@ModelAttribute("editForm") @Valid Book bookForm, BindingResult result, ModelMap model){
    if(result.hasErrors()){
        return "/admin/book";
    }

    LOG.info("Logged modified date before Save object: " + bookForm.getModifyDate());
    LOG.info("Logged author before Save object: " + bookForm.getAuthor());

    bookService.saveBook(bookForm);

    LOG.info("Logged modified date after Save object: " + bookForm.getModifyDate());
    LOG.info("Logged author after Save object: " + bookForm.getAuthor());

    return "admin/book";
}

我的書.jsp:

<form:form method="post" modelAttribute="editForm" >
        <div class="row border py-4">
            <div class="col-sm-6">
                <spring:bind path="title">
                    <div class="form-group">
                        <form:label path="title" for="title">Book title</form:label>
                        <form:input path="title" type="text" class="form-control" id="title" cssErrorClass="form-control border border-danger"/>
                    </div>
                </spring:bind>
                <spring:bind path="author">
                    <div class="form-group">
                        <form:label path="author" for="author">Author</form:label>
                        <form:input path="author" type="text" class="form-control" id="author" cssErrorClass="form-control border border-danger"/>
                    </div>
                </spring:bind>
                <p class="form-group">Last modified date: ${editForm.modifyDate}</p>

            </div>
    </form:form>

圖書實體:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", updatable = false, nullable = false)
private long id;

@Column(nullable = false)
@NotEmpty(message = "This field is required.")
private String title;

@Column(nullable = false)
private int quantity;

@Column(nullable = false)
private int availability;

@UpdateTimestamp
private LocalDateTime modifyDate;

@CreationTimestamp
@Column(name="create_date", updatable = false, nullable = false)
private LocalDateTime createDate;

從 GET 方法登錄:

2019-07-24 14:32:57.065 DEBUG 16119 --- [nio-8080-exec-6] org.hibernate.SQL                        : select book0_.id as id1_0_0_, book0_.availability as availabi2_0_0_, book0_.create_date as create_d3_0_0_, book0_.modify_date as modify_d4_0_0_, book0_.quantity as quantity5_0_0_, book0_.title as title6_0_0_, book0_.author as author7_0_0_ from book book0_ where book0_.id=?
Hibernate: select book0_.id as id1_0_0_, book0_.availability as availabi2_0_0_, book0_.create_date as create_d3_0_0_, book0_.modify_date as modify_d4_0_0_, book0_.quantity as quantity5_0_0_, book0_.title as title6_0_0_, book0_.author as author7_0_0_ from book book0_ where book0_.id=?
2019-07-24 14:32:57.068 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [10003]
2019-07-24 14:32:57.072 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([availabi2_0_0_] : [INTEGER]) - [56]
2019-07-24 14:32:57.072 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([create_d3_0_0_] : [TIMESTAMP]) - [2019-07-24T14:32:47.161]
2019-07-24 14:32:57.073 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([modify_d4_0_0_] : [TIMESTAMP]) - [2019-07-24 14:32:47.161]
2019-07-24 14:32:57.073 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([quantity5_0_0_] : [INTEGER]) - [56]
2019-07-24 14:32:57.073 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([title6_0_0_] : [VARCHAR]) - [Book title]
2019-07-24 14:32:57.073 TRACE 16119 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([author7_0_0_] : [VARCHAR]) - [Philippa Gregory]
2019-07-24 14:32:57.078  INFO 16119 --- [nio-8080-exec-6] c.s.s.web.controller.AdminController     : Logged modified date once page is loaded: 2019-07-24 14:32:47.161

從 POST 方法登錄

2019-07-24 14:34:12.840 DEBUG 16119 --- [nio-8080-exec-5] org.hibernate.SQL                        : select book0_.id as id1_0_0_, book0_.availability as availabi2_0_0_, book0_.create_date as create_d3_0_0_, book0_.modify_date as modify_d4_0_0_, book0_.quantity as quantity5_0_0_, book0_.title as title6_0_0_, book0_.author as author7_0_0_ from book book0_ where book0_.id=?
Hibernate: select book0_.id as id1_0_0_, book0_.availability as availabi2_0_0_, book0_.create_date as create_d3_0_0_, book0_.modify_date as modify_d4_0_0_, book0_.quantity as quantity5_0_0_, book0_.title as title6_0_0_, book0_.author as author7_0_0_ from book book0_ where book0_.id=?
2019-07-24 14:34:12.840 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [10003]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([availabi2_0_0_] : [INTEGER]) - [56]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([create_d3_0_0_] : [TIMESTAMP]) - [2019-07-24T14:32:47.161]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([modify_d4_0_0_] : [TIMESTAMP]) - [2019-07-24 14:32:47.161]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([quantity5_0_0_] : [INTEGER]) - [56]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([title6_0_0_] : [VARCHAR]) - [Book title]
2019-07-24 14:34:12.841 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([author7_0_0_] : [VARCHAR]) - [Philippa Gregory]
2019-07-24 14:34:12.843  INFO 16119 --- [nio-8080-exec-5] c.s.s.web.controller.AdminController     : Logged modified date before Save object: null
2019-07-24 14:34:12.843  INFO 16119 --- [nio-8080-exec-5] c.s.s.web.controller.AdminController     : Logged author before Save object: Book title 2
2019-07-24 14:34:12.874 DEBUG 16119 --- [nio-8080-exec-5] org.hibernate.SQL                        : update book set availability=?, modify_date=?, quantity=?, title=?, author=? where id=?
Hibernate: update book set availability=?, modify_date=?, quantity=?, title=?, author=? where id=?
2019-07-24 14:34:12.876 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [56]
2019-07-24 14:34:12.877 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [TIMESTAMP] - [Wed Jul 24 14:34:12 CEST 2019]
2019-07-24 14:34:12.877 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [56]
2019-07-24 14:34:12.877 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [Book title 2]
2019-07-24 14:34:12.878 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [Philippa Gregory]
2019-07-24 14:34:12.878 TRACE 16119 --- [nio-8080-exec-5] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [BIGINT] - [10003]
2019-07-24 14:34:12.884  INFO 16119 --- [nio-8080-exec-5] c.s.s.web.controller.AdminController     : Logged modified date after Save object: null
2019-07-24 14:34:12.885  INFO 16119 --- [nio-8080-exec-5] c.s.s.web.controller.AdminController     : Logged author after Save object: Book title 2

提交表單時,“修改日期”字段為空,而“標題”則可以。 有人可以向我解釋為什么嗎? 這顯示在控制台日志中。

看法:

提交前

提交后

嘗試為該LocalDateTime字段顯式添加一個反序列化器:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)  
@CreationTimestamp
@Column(name="create_date", updatable = false, nullable = false)
private LocalDateTime createDate;

還有你的反序列化類:

public class LocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {

    @Override
    public LocalDate deserialize(JsonParser jsonParser, DeserializationContext ctx)
            throws IOException, JsonProcessingException {
        // parse the String date into LocalDateTime object as it fits you
    }

}

您忘記在 My book.jsp 中添加 modify_date 字段:因為只有字段日期會發布,因為它是新請求,並且您保存在模型中的所有舊字段都不會隨請求一起發布。 所以有兩個選項添加輸入文本字段來更改日期。

或者您將在保存之前手動設置日期

 bookForm.setModifyDate(new Date());
 bookService.saveBook(bookForm); 

或者,如果您想顯示本地日期和時間,也可以添加 @UpdateTimestamp

@UpdateTimestamp
private LocalDateTime modifyDate;
@UpdateTimestamp
private LocalDateTime modifyDate;

這將完成工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM