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

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

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

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

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

Q&A

解決済

1回答

9317閲覧

画面遷移後の検索入力値保持の方法

退会済みユーザー

退会済みユーザー

総合スコア0

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

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

0グッド

0クリップ

投稿2020/07/22 08:29

あいまい検索(例:「田」と入力)を行った際に検索結果画面に遷移すると、テキストに入力した「田」が消えてしまいます。
またプルダウン選択の部署名検索も同様です。
遷移後も何で検索をかけたかが分かるように保持した状態にしたいのですが、どのように記述すればよいのでしょうか。

コード <div class="title">社員名検索</div> <div class="form"> <form method="post" th:action="@{/find}"> <input type="text" name="empName" /> <input type="submit" value="検索" /> </form> </div> <div class="title">部署名検索</div> <div class="form"> <form method="post" th:action="@{/findDept}"> <select name="deptId"> <option value="1" selected="selected">営業部</option> <option value="2">経理部</option> <option value="3">総務部</option> </select> <input type="submit" value="検索" /> </form> </div>
コード <table class="emp_list_table"> <tr> <th class="empId">社員ID</th> <th class="empName">社員名</th> <!-- <th:block th:if="${session.employee.authority == 2}"> --> <th class="gender">性別</th> <th class="address">住所</th> <th class="birthday">生年月日</th> <th class="authority">権限</th> </th:block> <th class="deptName">部署名</th> <!-- <th:block th:if="${session.employee.authority == 2}"> --> <th class="button" >変更</th> <th class="button" >削除</th> </th:block> </tr> <tr th:each="employee:${employees}"> <td th:text="${employee.empId}"></td> <td th:text="${employee.empName}"></td> <!-- <th:block th:if="${session.employee.authority == 2}"> --> <td> <div th:if="${employee.gender == 1}">男性</div> <div th:if="${employee.gender == 2}">女性</div> </td> <td th:text="${employee.address}"></td> <td th:text="${#dates.format(employee.birthday, 'yyyy/MM/dd')}"></td> <td> <div th:if="${employee.authority == 1}">一般</div> <div th:if="${employee.authority == 2}">管理者</div> </td> </th:block> <td th:text="${employee.department.deptName}"></td> <!-- <th:block th:if="${session.employee.authority == 2}"> --> <td class="button"> <form method="get" th:action="@{/update/input}"> <input type="hidden" name="empId" th:value="${employee.empId}" /> <input type="hidden" name="deptId" th:value="${employee.department.deptId}" /> <input type="submit" value="変更" /> </form> </td> <td class="button"> <form method="get" th:action="@{/delete/check}"> <input type="hidden" name="empId" th:value="${employee.empId}" /> <input type="submit" value="削除" class="delete" /> </form> </td> </th:block> </tr> </table>
コード /** * 社員名検索処理 * @param model * @param empName * @return 一覧画面 */ @RequestMapping(path = "/find", method = RequestMethod.POST) public String find(Model model, String empName) { //▼名前の検索実施 List<Employee> employees = employeeRepository.findByEmpNameLike("%" + empName + "%"); model.addAttribute("employees", employees); return "list/list"; } /** * 部署名検索処理 * @param model * @param deptId * @return 一覧画面 */ @RequestMapping(path = "/findDept", method = RequestMethod.POST) public String find(Model model, int deptId) { //▼部署の検索実施 Department department = new Department(); department.setDeptId(deptId); List<Employee> employees = employeeRepository.findByDepartment(department); model.addAttribute("employees", employees); return "list/list"; }

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

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

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

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

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

guest

回答1

0

ベストアンサー

Controllerにて、取得したempNameをModelへ格納していないのと

html

1<input type="text" name="empName" />

に、入力された値を出力する th:value="${empName}" がないので、表示されません。

<select>も同様に、選択した値をModelに格納して、th:select を使うことで実現できるでしょう。
th:select で検索すると使い方が出てきます。

投稿2020/07/22 09:44

A-pZ

総合スコア12011

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

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

退会済みユーザー

退会済みユーザー

2020/07/22 12:17

ご回答いただきありがとございました! 教えていただいた方法で試してみたところ無事解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問