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

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

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

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

Thymeleaf

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

Spring Boot

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

Q&A

解決済

1回答

5728閲覧

ページング、次のページで400 bad request

Yoshi--

総合スコア62

Java

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

Thymeleaf

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

Spring Boot

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

0グッド

0クリップ

投稿2017/07/21 02:33

編集2017/07/21 03:31

コントローラー

java

1 2 3package com.example.konkatsu.web; 4 5import java.io.IOException; 6import java.sql.Date; 7import java.util.ArrayList; 8import java.util.Base64; 9import java.util.List; 10 11import org.springframework.beans.BeanUtils; 12import org.springframework.beans.factory.annotation.Autowired; 13import org.springframework.data.domain.Page; 14import org.springframework.data.domain.Pageable; 15import org.springframework.data.web.PageableDefault; 16import org.springframework.security.core.annotation.AuthenticationPrincipal; 17import org.springframework.stereotype.Controller; 18import org.springframework.ui.Model; 19import org.springframework.validation.BindingResult; 20import org.springframework.validation.annotation.Validated; 21import org.springframework.web.bind.annotation.GetMapping; 22import org.springframework.web.bind.annotation.ModelAttribute; 23import org.springframework.web.bind.annotation.PostMapping; 24import org.springframework.web.bind.annotation.RequestMapping; 25import org.springframework.web.bind.annotation.RequestParam; 26import org.springframework.web.multipart.MultipartFile; 27 28import com.example.konkatsu.domain.Profile; 29import com.example.konkatsu.service.LoginUserDetails; 30import com.example.konkatsu.service.ProfileService; 31 32 33@Controller 34//「URL」の接頭辞をkonkatsuに設定(このクラスで呼ばれたpostなどは/konkatsu/(path)となる) 35@RequestMapping("konkatsu") 36 37public class KonkatsuController { 38 @Autowired 39 ProfileService profileService; 40 41 @ModelAttribute //@ModelAttributeを付けたメソッド内でProfileFormを初期化 42 //@ModelAttributeがついたメソッドは、@PostMappingでマッピングされたメソッドの前に実行され 43 //「返り値」は自動で「Model」に追加される(この例だと「list」や「create」メソッドが呼ばれる前に「model.addAttribute(new ProfileForm())」相当の処理が行われる) 44 ProfileForm setUpForm() { 45 return new ProfileForm(); 46 } 47 48 49 @GetMapping //pathの指定がない場合は URL は/konkatsu 50 public String myPage(@AuthenticationPrincipal LoginUserDetails userDetails) { 51 return "konkatsu/myPage"; 52 } 53 54 55 @GetMapping(path = "profileForm") //URL は/konkatsu/profileFormとなる 56 String list(Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 57 List<Profile> profile = profileService.findAll(); 58 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 59 return "konkatsu/profileForm"; //遷移する画面の名前 60 } 61 62 63 //th:action="@{/konkatsu/createProfile}" の処理 64 @PostMapping(path = "createProfile") //URLが /konkatsu/createProfile となる 65 public String create(@Validated ProfileForm form, BindingResult bindingResult, Model model, 66 // 送信されたフォームの入力チェックを行う為に@Validatedアノテーションを付ける。 67 // これによりProfileFormに設定したBean Validationアノテーションが評価され、結果が隣の引数のBindingResultに格納される 68 @AuthenticationPrincipal LoginUserDetails userDetails) throws IOException{ 69 //@AuthenticationPrincipalをつけることでログイン中の[LoginUserDetails]オブジェクトを取得できる 70 71 System.out.println(form.getId()); 72 System.out.println(form.getName()); 73 System.out.println(form.getGenderId()); 74 System.out.println(form.getBirthday()); 75 System.out.println(form.getHeight()); 76 System.out.println(form.getOccupationId()); 77 System.out.println(form.getIncome()); 78 System.out.println(form.getText()); 79 System.out.println(form.getFile()); 80 81 if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る 82 System.out.println("エラー"); 83 return list(model); 84 } 85 86 87 Profile profile = new Profile(); 88 profile.setId(form.getId()); 89 profile.setName(form.getName()); 90 profile.setGenderId(form.getGenderId()); 91 profile.setBirthday(form.getBirthday()); 92 profile.setHeight(form.getHeight()); 93 profile.setOccupationId(form.getOccupationId()); 94 profile.setIncome(form.getIncome()); 95 profile.setText(form.getText()); 96 MultipartFile uploaded = form.getFile(); 97 byte[] image = uploaded.getBytes(); //アップロードファイルをbyte配列で取得 98 profile.setImage(image); 99 100 BeanUtils.copyProperties(form, profile);//ProfileFormをProfileにコピーする 101 profileService.create(profile, userDetails.getUser()); //ProfileをDBに追加する (ログインユーザー情報も) 102 103 return "/konkatsu/profileConfirm"; 104 105 } 106 107 108 @GetMapping(path = "usersList") //pathの指定がない場合は URL は/konkatsu 109 //htmlからidとして送られた値を@RequestParamにて取得SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 110 String usersList(@RequestParam Integer genderId, @PageableDefault (size=3) Pageable pageable, Model model){ 111 Page<Profile> profiles = profileService.findUsers(genderId, pageable); 112 113 //<ProfileForView>に<Profile>の値を代入処理を繰り返し 114 List<ProfileForView> profilesForView = new ArrayList<ProfileForView>(); 115 for(Profile profile: profiles){ 116 ProfileForView profileForView = new ProfileForView(profile); 117 profilesForView.add(profileForView); 118 } 119 120 121 model.addAttribute("page", profiles); 122 model.addAttribute("url", "/konkatsu/usersList"); 123 model.addAttribute("profiles", profilesForView); 124 return "konkatsu/profileList"; //遷移する画面の名前 125 } 126 127 128 129 @GetMapping(path = "myProfile") //pathの指定がない場合は URL は/konkatsu 130 String MyList(@RequestParam Integer id, Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 131 Profile profile = profileService.findProfile(id); 132 133 //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトProfileForViewを指定 134 model.addAttribute("profile", new ProfileForView(profile)); 135 return "konkatsu/myProfile"; //遷移する画面の名前 136 } 137 138 //View用のクラス 139 public class ProfileForView { 140 public Integer id; 141 public String name; 142 public Integer genderId; 143 public String gender; 144 public Date birthday; 145 public Integer height; 146 public String occupationName; 147 public Integer income; 148 public String text; 149 public String mail; 150 public String image; 151 152 //profileの値を代入 153 public ProfileForView(Profile profile){ 154 this.id = profile.getId(); 155 this.name = profile.getName(); 156 this.genderId = profile.getGenderId(); 157 this.gender = profile.getGender().getGender(); 158 this.birthday = profile.getBirthday(); 159 this.height = profile.getHeight(); 160 this.occupationName = profile.getOccupation().getOccupationName(); 161 this.income = profile.getIncome(); 162 this.text = profile.getText(); 163 this.mail = profile.getUser().getMail(); 164 //画像ファイルをBASE64形式でエンコード 165 this.image = Base64.getEncoder().encodeToString(profile.getImage()); 166 } 167 } 168}

profileList.html

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 22 <table class="table table-striped table-bordered table-condensed"> 23 <h2>登録ユーザー</h2> 24 <tr> 25 <th>写真</th> 26 <th>名前</th> 27 <th>性別</th> 28 <th>誕生日</th> 29 <th>身長(cm)</th> 30 <th>職業</th> 31 <th>年収(万円)</th> 32 <th>自己紹介</th> 33 <th>メールアドレス</th> 34 </tr> 35 36 <tr th:each="profile : ${profiles}"> 37 <!-- エンコードした画像の表示 --> 38 <td><img th:src="${'data:image/png;base64,' + profile.image}" 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 <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 46 <td th:text="${profile.mail}">メール</td> 47 <td th:text="${profile.text}">自己紹介</td> 48 </tr> 49 </table> 50 51 <div th:fragment='paginationbar'> 52 <ul> 53 <li th:class="${page.first} ? 'disabled':''" style="display: inline"> 54 <span th:if="${page.first}">←先頭</span> 55 <a th:if="${not page.first}" th:href="@{${url}(page=0)}">←先頭</a> 56 </li> 57 58 <!-- #numbers.sequenceは引数に指定した2つの数値の範囲で配列を生成 --> 59 <li th:each='i : ${#numbers.sequence(0, page.totalPages-1)}' 60 th:class="(${i}==${page.number})? 'active' : ''" 61 style="display: inline"> 62 <span th:if='${i}==${page.number}' th:text='${i+1}'>1</span> 63 <a th:if='${i}!=${page.number}' th:href="@{'/konkatsu/usersList'(page=${i})}"> 64 <span th:text='${i+1}'>1</span> 65 </a> 66 </li> 67 <li th:class="${page.last} ? 'disabled':''" style="display: inline"> 68 <span th:if="${page.last}">末尾➝</span> 69 <a th:if="${not page.last}" th:href="@{${url}(page=(${page.totalPages}-1))}">末尾➝</a> 70 </li> 71 </ul> 72 </div> 73 74 <form th:action="@{/konkatsu/myProfile}" method="get"> 75 <input type="hidden" name="id" th:value="${user.userId}" /> 76 <button type="submit" class="btn btn-lg btn-primary btn-block">Myページへ</button> 77 </form> 78</body> 79</html>

*名前やアドレスなどは全て架空のものです

イメージ説明

こんな感じで表示されるのですが

次のページにいこうと
2
を押すと

イメージ説明

が表示されてしまいます

URLはhttp://localhost:8080/konkatsu/userList?page=1

が表示されています。

genderIDをパラメータで送るとすると
hiddenで送れば良いのでしょうか?
どこに書けば良いのでしょうか??

<form th:action="@{/konkatsu/usersList}" method="get"> <input type="hidden" name="genderId" th:value="${profile.genderId}" /> <button type="submit" class="btn btn-lg btn-primary btn-block">登録ユーザー一覧を見る</button> </form>

参考になる資料などがあれば
教えていただきたいです。

よろしくお願いします。

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

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

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

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

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

ahodana

2017/07/21 03:30

エラーは 404 ではなく、400 bad request
Yoshi--

2017/07/21 03:31

失礼いたしました、修正いたしました
guest

回答1

0

ベストアンサー

@RequestParamuの設定で

required=false

とすればそのエラーはでなくなるはずです。

未設定時は 必須になります

また、

(page=2)を(page=2,genderId=0)とすることでパラメーターに追加できます

投稿2017/07/21 03:38

編集2017/07/21 03:46
ahodana

総合スコア77

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

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

Yoshi--

2017/07/21 03:45

ありがとうございます! しかし次はこんなエラーとなってしまいました。 There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "#numbers.sequence(0, page.totalPages-1)"
Yoshi--

2017/07/21 03:52

表示されるようになりました! 助かりました。 本当にありがとうございます!!!!!!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問