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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

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

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

214閲覧

エラー画面のカスタマイズをしたい springboot

Yoshi--

総合スコア62

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Thymeleaf

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

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2017/08/04 02:49

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="utf-8" /> 5<title>婚活サイト</title> 6<link rel="stylesheet" 7 href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 8 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}" /> 9<link rel="stylesheet" 10 href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 11 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}" /> 12</head> 13 14<body th:with="user=${#authentication.principal.user}"> 15 <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> 16 17 <h2> 18 <span th:text="${user.mail}">メアド</span> さん、ログイン中 19 </h2> 20 21 <table class="table table-striped table-bordered table-condensed"> 22 <h2>登録ユーザー 一覧</h2> 23 <tr> 24 <th>写真</th> 25 <th>名前</th> 26 <th>性別</th> 27 <th>誕生日</th> 28 <th>身長(cm)</th> 29 <th>職業</th> 30 <th>年収(万円)</th> 31 <th>メールアドレス</th> 32 <th>自己紹介</th> 33 </tr> 34 35 <tr th:each="profile : ${profiles}"> 36 <!-- エンコードした画像の表示 --> 37 <td><img th:src="${'data:image/png;base64,' + profile.image}" 38 width="250" height="150" /></td> 39 <td th:text="${profile.name}">山田</td> 40 <td th:text="${profile.gender}"></td> 41 <td th:text="${profile.birthday}">誕生日</td> 42 <td th:text="${profile.height}">170</td> 43 <td th:text="${profile.occupationName}">職業</td> 44 <td th:text="${profile.income}">年収</td> 45 <td th:text="${profile.mail}">メール</td> 46 <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 47 <td th:text="${profile.text}">自己紹介</td> 48 <td> 49 <form th:action="@{/konkatsu/createFavorite}" method="post"> 50 <input type="hidden" name="userId" th:value="${user.userId}" /> <input 51 type="hidden" name="favoriteUserId" th:value="${profile.id}" /> 52 <button type="submit"></button> 53 <br/>お気に入りにする 54 </form> 55 </td> 56 <td> 57 <form th:action="@{/konkatsu/deleteFavorite}" method="post"> 58 <input type="hidden" name="userId" th:value="${user.userId}" /> <input 59 type="hidden" name="favoriteUserId" th:value="${profile.id}" /> 60 <button type="submit">×</button> 61 <br/>お気に入りをやめる 62 </form> 63 </td> 64 </tr> 65 </table> 66 67 <div th:fragment='paginationbar'> 68 <ul> 69 <li th:class="${page.first} ? 'disabled':''" style="display: inline"> 70 <span th:if="${page.first}">←先頭</span> <a th:if="${not page.first}" 71 th:href="@{${url}(searchName=${param.searchName},genderId=${param.genderId}, page=0)}">←先頭</a> 72 </li> 73 74 <!-- #numbers.sequenceは引数に指定した2つの数値の範囲で配列を生成 --> 75 <li th:each='i : ${#numbers.sequence(0, page.totalPages-1)}' 76 th:class="(${i}==${page.number})? 'active' : ''" 77 style="display: inline"><span th:if='${i}==${page.number}' 78 th:text='${i+1}'>1</span> <a th:if='${i}!=${page.number}' 79 th:href="@{${url} (searchName=${param.searchName}, genderId=${param.genderId}, page=${i})}"> 80 <span th:text='${i+1}'>1</span> 81 </a></li> 82 <li th:class="${page.last} ? 'disabled':''" style="display: inline"> 83 <span th:if="${page.last}">末尾➝</span> <a th:if="${not page.last}" 84 th:href="@{${url}(searchName=${param.searchName}, genderId=${param.genderId}, page=(${page.totalPages}-1))}">末尾➝</a> 85 </li> 86 </ul> 87 </div> 88 89 <form th:action="@{/konkatsu/myProfile}" method="get"> 90 <input type="hidden" name="id" th:value="${user.userId}" /> 91 <button type="submit" class="btn btn-lg btn-primary btn-block">Myページへ</button> 92 </form> 93 94</body> 95</html> 96

お気に入り機能を作成中なのですが
一度お気に入りにして
もう一度お気に入りにしようとすると
当然このようなエラーが出ます

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri Aug 04 11:39:14 JST 2017 There was an unexpected error (type=Internal Server Error, status=500). could not execute statement; SQL [n/a]; constraint [favorite_pkey]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

そのためこのエラーの時に

すでにお気に入りされています <トップページに戻る>

みたいな画面を表示したいのですが

どのようなやり方があるのでしょうか??

少しでいいのでお教えください

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

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

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

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

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

guest

回答1

0

ベストアンサー

Controllerに@ExceptionHandlerを使ったメソッドを実装しましょう。

または、お気に入りを押された時に、すぐにINSERT処理を実行するのではなく、SELECT処理をしてヒット数が0件ならINSERTを実行する。1件以上ヒットしたなら登録済みのメッセージを表示して同じ画面を表示するのがスマートでしょうか。

Spring 例外ハンドリング

ないしは

Spring ExceptionHandler

で色々参考になる情報が見つかるでしょう。

投稿2017/08/04 04:27

A-pZ

総合スコア12011

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

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

Yoshi--

2017/08/04 04:34

回答ありがとうございます!!!!! 参考にしてやってみます。 ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問