前提
ここに質問の内容を詳しく書いてください。
(例)
EclipseでThymeleafを使ってto doリストのようなシステムを作っています。
日付入力の機能を実装中に以下のエラーメッセージが発生しました。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 日付入力機能を動作するようにする
発生している問題・エラーメッセージ
date型なのですが日付欄何を入力しても1970年1月1日になります
エラーメッセージ
該当のソースコード
java ```import java.sql.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.FutureOrPresent; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import lombok.Data; import lombok.NoArgsConstructor; @Entity @Table(name ="tododata") @Data @NoArgsConstructor public class TodoEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column private Integer id; @Column(nullable = false) @NotNull private Date date; @Column(length = 100,nullable = false) @NotBlank @Size(max=30,min=0) private String task; @Column(length = 8 , nullable = false) private String progless; @Column(length = 4, nullable = false) private String priority; @Column(nullable = false) @FutureOrPresent private Date deadline; } java ``` コード ```import java.sql.Date; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class TodoController { @Autowired TodoRepository repository; @RequestMapping("/") public ModelAndView index(ModelAndView mv) { List<TodoEntity> list = repository.findAll(); mv.addObject("list", list); mv.setViewName("index"); return mv; } @RequestMapping("/write") public ModelAndView write(ModelAndView mv) { mv.addObject("title", "追加画面"); mv.addObject("form", new TodoEntity()); mv.setViewName("write"); return mv; } @RequestMapping("/insert") @Transactional(readOnly = false) public ModelAndView insert(@ModelAttribute("form") @Validated TodoEntity entity, BindingResult result, ModelAndView mv) { if (result.hasErrors()) { mv.setViewName("write"); mv.addObject("title", entity.getId() == null ? "追加" : "更新"); return mv; } entity.setDate(new Date (0)); repository.saveAndFlush(entity); return new ModelAndView("redirect:/"); } @RequestMapping("/show") public ModelAndView show(ModelAndView mv) { List<TodoEntity> list = repository.findAll(); mv.addObject("list", list); mv.setViewName("show"); return mv; } @RequestMapping("/data/{id}") public ModelAndView data( @PathVariable Integer id, ModelAndView mv) { return set(mv, id, "詳細", "data"); } private ModelAndView set(ModelAndView mv, int id, String title, String template) { Optional<TodoEntity> data = repository.findById(id); if (!data.isPresent()) { return new ModelAndView("redirect:/"); } mv.addObject("form", data.get()); mv.addObject("title", title); mv.setViewName(template); return mv; } @RequestMapping("/update/{id}") public ModelAndView update( @PathVariable Integer id, ModelAndView mv) { return set(mv, id, "更新", "write"); } @RequestMapping("/delete/{id}") public ModelAndView delete(@PathVariable Integer id, ModelAndView mv) { return set(mv, id, "削除確認", "data"); } @RequestMapping("/delete") @Transactional(readOnly = false) public ModelAndView remove(@RequestParam("id") Integer id) { repository.deleteById(id); return new ModelAndView("redirect:/"); } @RequestMapping("/search") public ModelAndView search(ModelAndView mv) { List<TodoEntity> list = repository.findAll(); mv.addObject("list", list); mv.setViewName("search"); return mv; } @RequestMapping("/find") public ModelAndView search( @RequestParam("key") String key, ModelAndView mv) { List<TodoEntity> list = repository.findByTaskLike("%" + key + "%"); mv.addObject("list", list); mv.setViewName("search"); return mv; } } ```java ソースコード ```Java <!DOCTYPE html> <html xmln:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>トップ画面</title> </head> <body> <h3>toDoリスト</h3> <table th:if="${list.size() > 0}" border="1" cellspacing="0" cellpadding="0"> <tr> <th> ID </th> <th> 日付 </th> <th> 内容 </th> <th> 優先度 </th> <th> 進捗 </th> <th> 締め切り </th> <th> 詳細 </th> <th> 更新 </th> <th> 削除 </th> </tr> <tr th:each="e : ${list}"> <div th:if="${e.progless!='完了'}"> <td th:text="${e.id}"></td> <td th:text="${#dates.format(e.date,'yyyy/MM/dd')}"> </td> <td th:text="${e.task}"></td> <td th:text="${e.priority}"></td> <td th:text="${e.progless}"></td> <td th:text="${#dates.format(e.deadline,'yyyy/MM/dd')}"> </td> <td><a th:href="@{/data/{x}(x=${e.id})}">表示</a></td> <td><a th:href="@{/update/{x}(x=${e.id})}">更新</a></td> <td><a th:href="@{/delete/{x}(x=${e.id})}">削除</a></td> </tr> </div> </table> <a th:href="@{/write}">追加</a> <a th:href="@{/show}">一覧表示</a> <a th:href="@{/search}">検索</a> </body> </html>```ここに言語を入力 コード ```<!DOCTYPE html> <html xmln:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <style type="text/css"> span.err { color: blueviolet; } </style> <title th:text="${title}"></title> </head> <body> <h2 th:text="${title}"></h2> <form th:action="@{/insert}" th:object="${form}" method="post"> <p>日 付 <input type="date" name="date" th:field="*{date}" /> </p> <p>内 容 <input type="text" name="task" th:field="*{task}" /> <span th:if="${#fields.hasErrors('task')}" th:errors="*{task}" class="err"></span> </p> <p>優 先 度 <select name="priority"> <option value="高" selected>高</option> <option value="中">中</option> <option value="低">低</option> </select></p> <p>進 捗 <select name="progless"> <option value="未完了" selected>未完了</option> <option value="完了">完了</option> </select></p> <p>締め切り <input type="date" name="deadline" th:field="*{deadline}" /> <span th:if="${#fields.hasErrors('deadline')}" th:errors="*{deadline}" class="err"></span></p> <input type="submit" value="送信" /> <input th:if="${title}=='更新画面'" type="hidden" th:field="*{id}" /> </form> <p><a th:href="@{/}">トップへ戻る</a></p> </body> </html> ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー