いつもお世話になっております。
springbootで会員検索画面を作成しました。
First画面(会員検索画面)を開き、検索する名前とパスワードを入力し
検索ボタンを押下しましたが検索がうまくできず、下記のエラーが出ました
2017-05-24 20:41:46.879 INFO 1425 --- [nio-8080-exec-5] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
listは[]
2017-05-24 20:41:49.075 ERROR 1425 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet dispatcherServlet threw exception
ログの結果である
listは[]
は
java
1 //firstPost画面呼び出し 2 @RequestMapping(value="/First",method=RequestMethod.POST) 3 public ModelAndView FirstPost(@ModelAttribute("formModel") String newaccount,ModelAndView mav){ 4 List<Newaccount> list = dataAccessRepository.findByName(newaccount); 5 System.out.println("listは"+list); 6 mav.addObject("datalist",list); 7 return mav; 8}
の結果であり、listの結果が空でした。
原因が不明ですのでご教授よろしくお願いいたします。
java
1package com.tuyano.springboot.app; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7import org.springframework.http.MediaType; 8import org.springframework.stereotype.Controller; 9import org.springframework.transaction.annotation.Transactional; 10import org.springframework.ui.Model; 11import org.springframework.web.bind.annotation.ModelAttribute; 12import org.springframework.web.bind.annotation.RequestBody; 13import org.springframework.web.bind.annotation.RequestMapping; 14import org.springframework.web.bind.annotation.RequestMethod; 15import org.springframework.web.bind.annotation.RequestParam; 16import org.springframework.web.bind.annotation.ResponseBody; 17import org.springframework.web.servlet.ModelAndView; 18import com.fasterxml.jackson.databind.SerializationFeature; 19import com.fasterxml.jackson.core.JsonProcessingException; 20import com.fasterxml.jackson.databind.ObjectMapper; 21import com.tuyano.springboot.app.Emp; 22import com.tuyano.springboot.app.EmpRepository; 23import com.tuyano.springboot.dateaccess.DataAccessRepository; 24import com.tuyano.springboot.dateaccess.Newaccount; 25 26 27 28 29@Controller 30@EnableAutoConfiguration 31public class Test { 32 33 @Autowired //リポジトリを紐づけます 34 EmpRepository repository; 35 @Autowired //リポジトリを紐づけます 36 DataAccessRepository dataAccessRepository; 37 @RequestMapping(value="/") 38 @ResponseBody 39 public String home(){ 40 //json値格納変数 41 String jsonvalue; 42 //全件取得します 43 Iterable<Emp> list = repository.findAll(); 44 45 // 取得した内容を出力します 46 for(Emp emp: list){ 47 //Jacksonでオブジェクトを JSON に変換 48 ObjectMapper objectMapper = new ObjectMapper(); 49 try { 50 jsonvalue = objectMapper.writeValueAsString(emp); 51 System.out.println("jsonの値"+jsonvalue); 52 return jsonvalue; 53 }catch (JsonProcessingException e) { 54 e.printStackTrace(); 55 } 56 } 57 return null; 58 59 } 60 //firstGet画面呼び出し 61 @RequestMapping(value="/First",method=RequestMethod.GET) 62 public String FirstGet(){ 63 64 return null; 65 } 66 //firstPost画面呼び出し 67 @RequestMapping(value="/First",method=RequestMethod.POST) 68 public ModelAndView FirstPost(@ModelAttribute("formModel") String newaccount,ModelAndView mav){ 69 List<Newaccount> list = dataAccessRepository.findByName(newaccount); 70 System.out.println("listは"+list); 71 mav.addObject("datalist",list); 72 return mav; 73} 74 //NewAccountGet画面呼び出し 75 @RequestMapping(value="/NewAccount",method=RequestMethod.GET) 76 public String NewAccountGet(){ 77 78 return null; 79 } 80 //NewAccountPost画面呼び出し 81 @RequestMapping(value="/NewAccount",method=RequestMethod.POST) 82 @Transactional(readOnly=false) 83 public ModelAndView NewAccountPost(@ModelAttribute("formModel") Newaccount newaccount,ModelAndView mav){ 84 System.out.println("newaccountは"+newaccount); 85 dataAccessRepository.saveAndFlush(newaccount); 86 return new ModelAndView("redirect:/NewAccount"); 87 } 88 //MemberGet画面呼び出し 89 @RequestMapping(value="/Member",method=RequestMethod.GET) 90 public String MemberGet(){ 91 92 return null; 93 } 94 //MemberPost画面呼び出し 95 @RequestMapping(value="/Member",method=RequestMethod.POST) 96 public ModelAndView MemberPost(@ModelAttribute("formModel") Newaccount newaccount,ModelAndView mav){ 97 System.out.println("newaccountは"+newaccount); 98 dataAccessRepository.saveAndFlush(newaccount); 99 return new ModelAndView("redirect:/NewAccount"); 100 } 101 102}
java
1package com.tuyano.springboot.dateaccess; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.GeneratedValue; 6import javax.persistence.Id; 7 8 9@Entity// このクラスはEntityとして登録しますよ、とspringに教えてます 10public class Newaccount { 11 12 @Id// プライマリーキーのものに設定してください 13 @GeneratedValue// 主に数字に対して、順番に一意に設定しますよ、の意味 14 private Integer ID; 15 16 @Column// ただの変数じゃなくて、DBのカラムだよ、の意味 17 private String name; 18 @Column// 19 private int password; 20 @Column// 21 private String address; 22 // setter & getter --------------------- 23 public Integer getID() { 24 return ID; 25 } 26 public String getName(){ 27 return name; 28 } 29 public int getPassword(){ 30 return password; 31 } 32 public String getAddress(){ 33 return address; 34 } 35 36 public void setEmpID(Integer ID) { 37 this.ID = ID; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 public void setPassword(int password) { 45 this.password = password; 46 } 47 48 public void setAddress(String address) { 49 this.address = address; 50 } 51 52 53 54 // constructor -------------------------- 55 public Newaccount(){ 56 super(); 57 } 58 59 public Newaccount(Integer ID, String name,int password,String address){ 60 super(); 61 this.ID =ID; 62 this.name = name; 63 this.password = password; 64 this.address = address; 65 } 66 67}
java
1package com.tuyano.springboot.dateaccess; 2 3import java.util.List; 4 5import org.springframework.data.jpa.repository.JpaRepository; 6import org.springframework.stereotype.Repository; 7 8 9 10@Repository 11public interface DataAccessRepository extends JpaRepository<Newaccount, Integer>{ 12 public List<Newaccount> findByName(String s); 13} 14
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<title>top page</title> 5<meta http-equiv="Content-Type" 6content="text/html" charset="UTF-8"/> 7 8<!-- css --> 9 10<style> 11 html { height: 100% } 12 body { height: 100%; margin: 0; padding: 0 } 13 #map { height: 100% } 14</style> 15 16 17 18 19</head> 20<body> 21 22 <h1 th:text="${title}">find Page</h1> 23 <table> 24 <form method="post" action="/First" th:object="${formModel}"> 25 <tr><td>名前</td> 26 <td><input type="text" id="name" name="name" size="20" th:value="${name}"/></td></tr> 27 <tr><td>パスワード</td> 28 <td><input type="text" id="password" name="password" size="20" th:value="${password}"/></td></tr> 29 <tr><td>住所</td> 30 <td><input type="text" id="address" name="address" size="20" th:value="${address}"/></td></tr> 31 <tr><td></td><td><input type="submit" /></td></tr> 32 </form> 33 </table> 34 <hr/> 35 <form action="urlForUpload" enctype="multipart/form-data" method="post"> 36 <div class="form-group"> 37 <label>■ファイル種類:</label> 38 <select id="select_file_type" name="select_file_type" required=""> 39 <option value="login-user">ログインユーザー</option> 40 <!-- アップロードするファイルを定義していく --> 41 </select> 42 </div> 43 <div class="form-group"> 44 <label>■ファイルパス:</label> 45 <input type="file" id="upload_file" name="upload_file" required="" /> 46 </div> 47 <div class="form-group"> 48 <input id="data_upload_button" type="submit" value="アップロード" /> 49 </div> 50 </form> 51 <table> 52<tr><th>名前</th><th>住所</th></tr> 53<tr th:each="obj :${datalist}"> 54<td th:text="${obj.name}"></td> 55 56</tr> 57</table> 58 59</body> 60</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/26 15:03