2017-06-29 7 views
0

私は最新の春と冬眠を使用しています。私は日時のJSPページを取得し、mysqlデータベースに挿入する必要があります。 Im TIMESTAMPを1フィールドのデータ型として使用しています。保存しようとすると、エラーは発生しませんが、「HTTPステータス[400] - [Bad Request]」と表示されます。春の日付と時刻を設定するmvc

最後に私が見つける、日付と時刻の形式での問題は、(注釈やMySQLデータ型またはJSPページであってもよい)があります。日付時刻の値を変更することなくフォームを更新しようとしています(パス= "startDateTime")。正常に更新されました。日付時刻(パス= "startDateTime")の値を変更しようとすると、「HTTPステータス[400] - [Bad Request]」と表示されます。

データベースに日付と時刻を更新する必要があります。私は多くの方法で試しましたが、失敗しました。この問題を解決するために私を助けてください。前もって感謝します。

Modelクラス

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"); 
} 

答えて

0

問題は、パラメータが春のMVCに送信され、あなたdateTodayタイプが日付で、文字列です。春のmvcはそれを変換する方法を知らない。

は、あなたはそれが更新するように設定されている、 があなたのhibernate設定ファイルの.xmlで

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

の下にご相談ください@InitBinderが必要ですか?それをチェックしてみてください。良い習慣を採用するためには

<property name="hbm2ddl.auto">update</property>

1

、私はJAVA 8のLocalDateTimeクラスを使用してお勧めします。 JAVA 8のLocalDateTimeクラスを使用する場合 だからこの

public class Survey{ 

//other declaration 

    @Column(name="startDateTime",columnDefinition="TIMESTAMP")  
    private LocalDateTime startDateTime; 
} 

のようなあなたのモデルクラスを変更し、あなたは、もはやその後@Temporal(TemporalType.TIMESTAMP)

を追加する必要がない、あなたはこの

ようなコンバータインタフェースを実装することにより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

よく学ぶことができ、私はまだ@CreationTimestamp@UpdateTimestamp注釈を使用して代わりに、日付時刻の更新を行うためにあなたのサイトのユーザーへの責任を残すなぜHibernateはあなたのためにそれを行うには許可しないことを示唆するようになりますHibernateは自動的にそれらの列を挿入し、更新するためのこのアプローチでは、この

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

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

のようにJPA @PrePersistと​​注釈を使用することによって、好ましくは休止または、あなたはもはやあなたの形でstartDateTimeフィールドを追加する必要がありません。

注:フォーム入力タイプのdatetime-localは、すべてのブラウザでサポートされていません。あなたのコントローラクラスで

+0

解決していないあなたはノートで述べたように、入力datetime型は、JSPで使用できなくなりました。どうすれば日時を取得できますか?私は日時を設定する必要がある、私は更新するために休止状態を必要としない。 '@ PreUpdate'の必要はありません、そうですか? – varman

+0

私はこの問題がJSPに関係しているのか疑問です。フォーム入力タイプdatetime-localは、Firefox、Internet Explorer 12およびそれ以前のバージョンではサポートされていません。 jquery datetime pickerを使用して日付と時刻を取得することをお勧めします。 jqueryのdatetimeピッカーがISO形式のdatetimeを取得しない場合は、コンバータインターフェイスを実装し、私の答えに記載されているようにクラスを登録する必要があります。 – Perry

+0

public ModelAndView saveSurvey(@RequestParam( "startDateTime")) String startDateTime){ LocalDateTime lds = LocalDateTime.parse(startDateTime);このメソッドはStringを現在の日付に変換します。しかし、私が 'public ModelAndView saveSurvey(@ModelAttribute(" surveyForm ")Survey survey){'を使用すると、それは動作しません – varman

0

使用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)); 
} 

希望これは

+0

私も望みますが、私はすでに試みました。働いていない – varman

関連する問題