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

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

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

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

Q&A

解決済

2回答

6196閲覧

springbootの検索結果画面について

heavyuseman

総合スコア42

Spring

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

0グッド

0クリップ

投稿2016/08/28 05:49

###前提・実現したいこと
springbootで会員検索結果画面を作成しています。
会員検索画面はfirst.index
会員検索結果画面はlogin.index
です。
使用しているDBはMysqlです。
DB上のカラムはID,名前,パスワードです。
会員検索画面で名前でDBに検索して、検索結果画面に検索結果が表示されてほしいのですが、結果
が画面上に表示されません。会員検索画面の情報(名前検索した情報)を会員検索結果画面に渡すにはどうすればいいでしょうか?
ご回答のほど宜しくお願い致します

ログ 2016-08-28 14:38:24.133 INFO 935 --- [ main] c.t.springboot.SokutakuhaiApplication : Started SokutakuhaiApplication in 7.929 seconds (JVM running for 8.709) 2016-08-28 14:38:34.566 INFO 935 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-08-28 14:38:34.567 INFO 935 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2016-08-28 14:38:34.590 INFO 935 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 23 ms 2016-08-28 14:38:34.681 INFO 935 --- [nio-8080-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory ModelAndView: reference to view with name 'first'; model is {title=Find Page, msg=サンプルです, value=, datalist=[com.tuyano.springboot.MyDataMongo@1df12e20]} param1234 [com.tuyano.springboot.MyDataMongo@6a4b9990] ModelAndView: reference to view with name 'first'; model is {title=Find Result, msg=[1234]の検索結果, value=1234, dataList=[com.tuyano.springboot.MyDataMongo@6a4b9990]} ModelAndView: reference to view with name 'login'; model is {title=Find Page, msg=loginです, value=loginページです}

java

1HeloController 2package com.tuyano.springboot; 3 4import java.util.List; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Controller; 8import org.springframework.transaction.annotation.Transactional; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.RequestMapping; 11import org.springframework.web.bind.annotation.RequestMethod; 12import org.springframework.web.bind.annotation.RequestParam; 13import org.springframework.web.servlet.ModelAndView; 14 15import com.tuyano.springboot.repositories.MyDataMongoRepository; 16 17@Controller 18public class HeloController { 19 20@Autowired 21MyDataMongoRepository repository; 22 23@RequestMapping(value="/",method=RequestMethod.GET) 24public ModelAndView index(@ModelAttribute("formModel")MyDataMongo mydatamongo,ModelAndView mav){ 25 mav.setViewName("index"); 26 mav.addObject("title","Find Page"); 27 mav.addObject("msg","サンプルです"); 28 List<MyDataMongo> list=repository.findAll(); 29 mav.addObject("datalist",list); 30 System.out.println(mav); 31 return mav; 32} 33@RequestMapping(value="/",method=RequestMethod.POST) 34 35public ModelAndView form( 36 @ModelAttribute("formModel")MyDataMongo mydatamongo,ModelAndView mav){ 37 repository.saveAndFlush(mydatamongo); 38 mav =new ModelAndView("redirect:/"); 39 System.out.println("mov"+mav); 40 return mav; 41} 42 43@RequestMapping(value="/first",method=RequestMethod.GET) 44public ModelAndView find(@ModelAttribute("formModel")MyDataMongo mydatamongo,ModelAndView mav){ 45 mav.setViewName("first"); 46 mav.addObject("title","Find Page"); 47 mav.addObject("msg","サンプルです"); 48 mav.addObject("value",""); 49 List<MyDataMongo> list=repository.findAll(); 50 mav.addObject("datalist",list); 51 System.out.println(mav); 52 return mav; 53} 54@RequestMapping(value="/first",method=RequestMethod.POST) 55public ModelAndView search(@RequestParam("name")String param,ModelAndView mav){ 56 mav.setViewName("first"); 57 58 if(param ==""){ 59 mav =new ModelAndView("redirect:/first"); 60 }else{ 61 System.out.println("param"+param); 62 mav.addObject("title","Find Result"); 63 mav.addObject("msg","["+param+"]の検索結果"); 64 mav.addObject("value",param); 65 List<MyDataMongo> list=repository.findByName(param); 66 System.out.println(list); 67 mav.addObject("dataList",list); 68 69 } 70 System.out.println(mav); 71 mav =new ModelAndView("redirect:/login"); 72return mav; 73} 74 75 76@RequestMapping(value="/login",method=RequestMethod.GET) 77public ModelAndView login(ModelAndView mav){ 78 mav.setViewName("login"); 79 mav.addObject("title","Find Page"); 80 mav.addObject("msg","loginです"); 81 mav.addObject("value","loginページです"); 82 System.out.println(mav); 83 return mav; 84} 85}

java

1MyDataMongo.java 2package com.tuyano.springboot; 3 4import java.util.Date; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.Table; 12 13 14 15 16@Entity 17@Table(name="Linefriend") 18public class MyDataMongo { 19@Id 20@GeneratedValue(strategy=GenerationType.AUTO) 21@Column 22private Long id; 23 24@Column 25private String name; 26@Column 27private String password; 28//private Date date; 29 30 31 32// 33public MyDataMongo() { 34 35this.name="田中"; 36this.password="田中"; 37} 38 39public void setId(Long id){ 40 41 this.id=id; 42 43} 44 45public Long getId(){ 46 47 return id; 48 49} 50 51 52public String getName(){ 53 54 return name; 55 56} 57 58public void setName(String name){ 59 60 this.name=name; 61 62} 63 64 65 66public String getPassword(){ 67 68 return password; 69 70} 71 72public void setPassword(String password){ 73 74 this.password=password; 75 76} 77 78//public Date getDate(){ 79 80 //return date; 81 82//} 83 84 85 86 87} 88 89

HTML

1・first.html 2<!DOCTYPE html> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>top page</title> 6<meta http-equiv="Content-Type" 7content="text/html" charset="UTF-8"/> 8</head> 9<body> 10 11 <h1 th:text="${title}">find Page</h1> 12 <p th:text="${msg}"></p> 13 <table> 14 <form method="post" action="/first"> 15 <tr><td>名前</td> 16 <td><input type="text" name="name" size="20" th:value="${value}"/></td></tr> 17 <tr><td>パスワード</td> 18 <td><input type="text" name="password" size="20" th:value="${value}"/></td></tr> 19 <tr><td></td><td><input type="submit"/></td></tr> 20 </form> 21 </table> 22 <hr/> 23 <table> 24 <tr><th>名前</th><th>メモ</th><th>日時</th></tr> 25 <tr th:each="obj :${dataList}"> 26 <td th:text="${obj.name}"></td> 27 28 29 </tr> 30 </table> 31 32 33</body> 34</html> 35</html>

HTML

1・login.html 2<!DOCTYPE html> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>top page</title> 6<meta http-equiv="Content-Type" 7content="text/html" charset="UTF-8"/> 8</head> 9<body> 10 11 <h1 th:text="${title}">find Page</h1> 12 <p th:text="${msg}"></p> 13 <table> 14 <tr><th>名前</th><th>メモ</th><th>日時</th></tr> 15 <tr th:each="obj :${dataList}"> 16 <td th:text="${obj.name}"></td> 17 18 19 </tr> 20 </table> 21 22 23</body> 24</html>

java

1MyDataMongoRepository.java 2package com.tuyano.springboot.repositories; 3 4import java.util.List; 5 6import org.springframework.data.jpa.repository.JpaRepository; 7import org.springframework.stereotype.Repository; 8 9import com.tuyano.springboot.MyDataMongo; 10 11 12@Repository 13public interface MyDataMongoRepository extends JpaRepository<MyDataMongo,String>{ 14 15public List<MyDataMongo> findByName(String s); 16 17 18} 19 20 21 22

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

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

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

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

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

guest

回答2

0

ベストアンサー

HeloControllerの68行目のコードに問題がありますね。

lang

1mav = new ModelAndView("redirect:/login");

さしあたっての問題は、ModelAndViewnewして再代入しまっているために、せっかくaddObjectで登録した情報が捨てられてしまっている点です。
これはSpring固有ではなくJavaの話になりますが、この操作は、新しい空のインスタンスを変数に代入することで、元々入っていたオブジェクトを捨てています。
一般的に、変数の再代入・再利用は避けるのが賢明です。(数値の場合の+=などは例外)

とりあえず下記のようにすれば表示できます。
意図的にリダイレクトをしたいのでなければ、これでできるようになります。

lang

1// mav = new ModelAndView("redirect:/login"); 2 3// redirect:/firstを有効にするために、elseブロックの最後に移動してください 4mav.setViewName("login");

投稿2016/08/28 07:32

編集2016/08/28 08:23
argius

総合スコア9388

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

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

heavyuseman

2016/08/29 09:41

ご回答して頂きありがとうございます。 mav = new ModelAndView("redirect:/login"); を削除し、else文の最後に mav.setViewName("login"); を記載したところ無事、検索結果が表示されました。 お忙しい中、ありがとうございました。
guest

0

HeloController の

mav =new ModelAndView("redirect:/login");

の行が不要ではないでしょうか。

投稿2016/08/28 09:25

A-pZ

総合スコア12011

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

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

heavyuseman

2016/08/29 09:40

ご回答して頂きありがとうございます。 mav = new ModelAndView("redirect:/login"); を削除し、else文の最後に mav.setViewName("login"); を記載したところ無事、検索結果が表示されました。 お忙しい中、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問