質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Q&A

解決済

1回答

27471閲覧

thymleafでhiddenした値をcontroller側で受け取れない

chocolate_pie

総合スコア26

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

0グッド

0クリップ

投稿2020/01/09 07:01

前提・実現したいこと

thymeleafで値だけを表示して、その値をそのままコントローラーで受け取りたいのですが、
デバックをするとhiddenで送ろうとした値がnullになってしまいます。

input type=textにすると正常に値が送信され、コントローラーにも問題がなかったので
thymeleafの書き方がおかしいということまでわかりましたが、書き方が調べても分かりません
でした。

発生している問題・エラーメッセージ

エラーメッセージ エラーメッセージはありませんが、コントローラーにthymeleafの値が渡っていません

該当のソースコード

HTML

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title>Insert title here</title> 6</head> 7<body> 8<h1>部目標編集</h1> 9 10<form method="post" 11 th:action="@{/deptgoal/complete/{id}(id=${deptgoal.id})}"> 12<table> 13 <tr> 14 <th>年度</th> 15 <th>部名</th> 16 <th>部目標</th> 17 </tr> 18 <tr th:object="${deptgoal}"> 19 <td> 20 <p th:text="*{year}"></p> 21 <!-- ここの値が送信されていません↓ --> 22 <input type="hidden" name="year" th:value="*{year}"> 23 </td> 24 <td> 25 <p th:text="*{deptid.deptname}"></p> 26 <!-- なぜかここの値は送信できています↓ --> 27 <input type="hidden" name="deptid" th:value="*{deptid.id}"> 28 <td><textarea name="deptgoal" th:field="*{deptgoal}" cols="40" rows="8" required></textarea></td> 29 </tr> 30 </table> 31 <input type="submit" value="登録する" /> 32 </form> 33 <button onclick="location.href='/admin/deptgoals'">戻る</button> 34 <form method="post" 35 th:action="@{/deptgoal/delete/{id}(id=${deptgoal.id})}"> 36 <button >削除</button></form> 37</body> 38</html>

コントローラ

java

1 @RequestMapping("/admin/deptgoals") 2 public String deptgoalShow(Model model) { 3 //既存目標の表示 4 List<DeptGoal> deptgoals = deptgoalservice.findCurrent(); 5 model.addAttribute("deptgoals", deptgoals); 6 return "admins/deptgoals"; 7 } 8 @RequestMapping("/admin/deptgoal/complete") 9 public String deptgoalNew(DeptGoalForm deptgoalform, Principal principal) { 10 //今ログインしている人のID 11 Authentication auth = (Authentication) principal; 12 UserMaster loginuser = (UserMaster) auth.getPrincipal(); 13 //時間取ってくる 14 Timestamp timestamp = new Timestamp(System.currentTimeMillis()); 15 16 DeptGoal deptgoal = new DeptGoal(); 17 DeptMaster deptid = deptmasterservice.findOne(deptgoalform.getDeptid()); 18 deptgoal.setDeptgoal(deptgoalform.getDeptgoal()); 19 deptgoal.setYear(deptgoalform.getYear()); 20 deptgoal.setDeptid(deptid); 21 deptgoal.setDeleteflg(false); 22 deptgoal.setUpdatedat(timestamp); 23 deptgoal.setUpdatedby(loginuser.getId()); 24 deptgoal.setCreatedat(timestamp); 25 deptgoalservice.save(deptgoal); 26 return "redirect:/admin/deptgoals"; 27 } 28

試したこと

・デバックしてどこまで値が取れているのか確認
・ほかの書き方を調べて実践(失敗)

補足情報(FW/ツールのバージョンなど)

・getメソッドの値は正常に取得できています
・year以外の値はコントローラーで正常に取得できています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。同じコードを書き直したら直りました。
書き方が同じだったのに謎です…。

変更前

HTML

1 <td> 2 <p th:text="*{year}"></p> 3 <!-- ここの値が送信されていません↓ --> 4 <input type="hidden" name="year" th:value="*{year}"> 5 </td>

変更後

HTML

1 <td> 2 <p th:text="*{year}"></p> 3           <input type="hidden" name="year" th:value="*{year}"> 4 </td> 5 6

投稿2020/01/10 02:17

chocolate_pie

総合スコア26

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問