简体   繁体   English

Spring MVC:将单独的时间和日期输入字段绑定到 LocalDateTime

[英]Spring MVC: Bind seperate time and date input field to LocalDateTime

I need a way to let the user specify a time and date and store it in a database.我需要一种方法让用户指定时间和日期并将其存储在数据库中。 The 'storing in the database' part works but I'm not sure how to let the user specify it. “存储在数据库中”部分有效,但我不确定如何让用户指定它。 Is it possible to bind a seperate time and date input field in the form to 1 LocalDateTime object, instead of using input type="datetime-local" since Firefox doesn't support it, or is there a better way?是否可以将表单中的单独时间和日期输入字段绑定到 1 LocalDateTime 对象,而不是使用input type="datetime-local"因为 Firefox 不支持它,或者有更好的方法吗?

One of the jobs of your controller is to map data from the client to the format needed by your DAO.控制器的工作之一是将来自客户端的数据映射到 DAO 所需的格式。

If client will only send date and time as separate fields, and DAO needs it as a combined value, then that transformation should be done by the controller.如果客户端仅将日期和时间作为单独的字段发送,而 DAO 需要将其作为组合值,则该转换应由控制器完成。

Sure, Spring will help with common transformations, but some transformations will require you to write code.当然,Spring 会帮助进行常见的转换,但有些转换需要您编写代码。

In this example, Spring can parse date from client to LocalDate , and time from client to LocalTime , then your controller can combine them and send a LocalDateTime to the DAO.在这个例子中,Spring 可以将日期从客户端解析为LocalDate ,将时间从客户端解析为LocalTime ,然后您的控制器可以将它们组合起来并将LocalDateTime发送到 DAO。

As your programs complexity increases, you'll find that it's rare for the controller input to exactly match the information needed by the DAO, and you have to do more in controllers.随着程序复杂性的增加,您会发现控制器输入很少与 DAO 所需的信息完全匹配,您必须在控制器中做更多的事情。 Live with it.和它一起生活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM