【问题标题】:Set date and time in Spring mvc在 Spring mvc 中设置日期和时间
【发布时间】:2017-12-02 19:41:55
【问题描述】:

我正在使用最新的 spring 和 hibernate。我需要从 jsp 页面获取日期和时间,并希望插入到 mysql 数据库中。我使用 TIMESTAMP 作为一个字段的数据类型。当我尝试保存时,没有错误,但显示“HTTP Status [400] – [Bad Request]”。

最后我发现,日期和时间格式有问题(可能在注解或MySQL数据类型或jsp页面)。因为我尝试在不更改日期时间值的情况下更新表单 (path="startDateTime")。它已成功更新。当我尝试更改日期时间 (path="startDateTime") 中的值时,它显示“HTTP 状态 [400] – [Bad Request]”。 p>

我需要将日期和时间更新到数据库。我尝试了很多方法,但都失败了。

模型类

public class Survey{

//other declaration

    @Column(name="startDateTime",columnDefinition="TIMESTAMP")      
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
    private Date startDateTime;
}

jsp页面

<spring:url value="/survey/save" var="saveURL"></spring:url>
<form:form action="${saveURL}" method="POST" modelAttribute="surveyForm">
//other stuffs
<form:input type="datetime-local" path="startDateTime" />
</form:form>

控制器

public ModelAndView saveSurvey(@ModelAttribute("surveyForm") Survey survey) {       
    surveyService.saveOrUpdate(survey);
    return new ModelAndView("redirect:/survey/list");
}

【问题讨论】:

    标签: java mysql spring hibernate jsp


    【解决方案1】:

    为了采用良好的做法,我建议使用 JAVA 8 LocalDateTime 类。 所以像这样修改你的模型类

    public class Survey{
    
    //other declaration
    
        @Column(name="startDateTime",columnDefinition="TIMESTAMP")      
        private LocalDateTime startDateTime;
    }
    

    使用 JAVA 8 LocalDateTime 类时,不再需要添加@Temporal(TemporalType.TIMESTAMP)

    之后,你需要通过像这样实现Converter接口来创建一个LocalDateTimeConverter类

    import org.springframework.core.convert.converter.Converter;
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public final class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
    
        private final DateTimeFormatter formatter;
    
        public LocalDateTimeConverter(String dateFormat) {
            this.formatter = DateTimeFormatter.ofPattern(dateFormat);
        }
    
        @Override
        public LocalDateTime convert(String source) {
            if (source == null || source.isEmpty()) {
                return null;
            }
    
            return LocalDateTime.parse(source, formatter);
        }
    }
    

    然后像这样在你的配置类中注册 LocalDateTimeConverter 类

    import org.springframework.context.annotation.Configuration;
    import org.springframework.format.FormatterRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @EnableWebMvc
    class WebMvcContext extends WebMvcConfigurerAdapter {
    
        @Override
        public void addFormatters(FormatterRegistry registry) {
            registry.addConverter(new LocalDateTimeConverter("yyyy-MM-dd'T'HH:mm:ss.SSS"));
        }
    }
    

    就是这样,我希望它能解决您的问题。 你可以了解更多here

    好吧,我仍然想建议,与其将更新日期时间的责任留给您的站点用户,不如让 hibernate 使用 hibernate 的 @CreationTimestamp@UpdateTimestamp 注释来为您执行此操作或最好使用 JPA @PrePersist@PreUpdate 这样的注释

    @Column(name="startDateTime")      
    @PrePersist
    private LocalDateTime startDateTime;
    
    @Column(name="updatedDateTime")      
    @PreUpdate
    private LocalDateTime updatedDateTime;
    

    使用这种方法,您不再需要在表单中添加 startDateTime 字段,因为 hibernate 会自动为您插入和更新这些列。

    注意:并非所有浏览器都支持表单输入类型 datetime-local。

    【讨论】:

    • 正如您在注释中提到的,输入类型 datetime 在 jsp 中不再可用。那么我怎样才能得到日期和时间呢?我需要设置日期和时间,我不需要休眠来更新。所以不需要@PreUpdate,对吧?
    • 我怀疑这个问题与 JSP 相关。 Firefox 和 Internet Explorer 12 及其早期版本不支持表单输入类型 datetime-local。我建议您使用 jquery 日期时间选择器来获取日期和时间。如果 jquery 日期时间选择器没有获取 ISO 格式的日期时间,您将需要实现 Converter 接口并像我在回答中所说的那样注册类,否则您不需要
    • public ModelAndView saveSurvey(@RequestParam("startDateTime") String startDateTime){ LocalDateTime lds = LocalDateTime.parse(startDateTime); 这个方法将字符串转换为日期。但是当我使用public ModelAndView saveSurvey(@ModelAttribute("surveyForm") Survey survey) {时,它不起作用
    • 这正是您应该实现 Converter 接口然后在配置类中注册实现它的类的原因,就像我在回答中开始的那样。这样@ModelAttribute("surveyForm") 调查问卷将为您工作
    • 其实我已经完成了。但它不会进入 public ModelAndView saveSurvey(@ModelAttribute("surveyForm") Survey survey) { 内部。但我试图在LocalDateTimeConverter 类中打印日期。当我提交按钮时,它工作正常,意味着它打印日期和时间。但它不会编译该方法。所以我认为,Survey survey 对象没有设置值。有没有我忘记的注释?正如你所说,我使用了private LocalDateTime startDateTime;
    【解决方案2】:

    问题是发送给spring mvc的参数是String,你的dateToday类型是date。 Spring mvc 不知道如何转换。

    你需要一个@InitBinder 下方咨询

    @InitBinder  
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, false));  
    }  
    

    【讨论】:

      【解决方案3】:

      在您的休眠配置文件 .xml 中,是否设置为更新?试试看。

      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;

      【讨论】:

        【解决方案4】:

        在你的控制器类中使用 initbinder

        @InitBinder
        public void dataBinding(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new 
                SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(LocalDateTime.class, "startDateTime", new CustomDateEditor(dateFormat, true));
        }
        

        希望能解决这个问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-09
          • 2021-04-03
          • 1970-01-01
          • 2013-12-28
          • 2011-11-02
          相关资源
          最近更新 更多