2016-12-24 7 views
0

CrudRepositoryのメソッドを使用して検索し、ウェブサイトにレコードを表示する簡単なボタンを作成します。 StudentRepository.javaそれはあなたにあまりにも些細に見えるかもしれませんので、私は、初心者だけど、私はそれにもかかわらず、アドバイスを求めている:)Spring MVC、CrudRepository、Thymeleafでの検索(テキストフィールドとボタン経由)

public interface StudentRepository extends CrudRepository<Student, Integer> { 
    Iterable<Student> findBySurname(String surname); 
} 

StudentService.java

public interface StudentService { 
    Iterable<Student> listStudentsBySurname(String surname); 
} 

StudentServiceImpl.java

@Service 
public class StudentServiceImpl implements StudentService { 
    private StudentRepository studentRepository; 

    @Autowired 
    public void setStudentRepository(StudentRepository studentRepository) { 
     this.studentRepository = studentRepository; 
    } 

    @Override 
    public Iterable<Student> listStudentsBySurname(String surname) { 
     return studentRepository.findBySurname(surname); 
    } 
} 

students.html

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head lang="en"> 

    <title></title> 

    <!--/*/ <th:block th:include="fragments/headerincStudent :: head"></th:block> /*/--> 
</head> 
<body> 
<div class="container"> 
    <!--/*/ <th:block th:include="fragments/headerStudent :: header"></th:block> /*/--> 

    <h2>Search for students</h2> 
    <form th:object="${student}" th:action="@{/students}" method="get"> 
     <input type="text" name="search" id="search" th:value="${search}"/> 
     <input type="submit" value="Search"/> 
     <div th:if="${not #lists.isEmpty(search)}"> 
      <h2>Students List</h2> 
      <table class="table table-striped"> 
       <tr> 
        <th>Id</th> 
        <th>Name</th> 
        <th>Surname</th> 
       </tr> 
       <tr th:each="student: ${search}"> 
        <td th:text="${student.idStudent}"><a href="/student/${student.idStudent}">idStudent</a></td> 
        <td th:text="${student.name}">name</td> 
        <td th:text="${student.surname}">surname</td> 
       </tr> 
      </table> 
     </div> 
    </form> 
</div> 
</body> 
</html> 

StudentController.java

@Controller 
public class StudentController { 
    private StudentService studentService; 

    @Autowired 
    public void setStudentService(StudentService studentService) { 
     this.studentService = studentService; 
    } 

    @RequestMapping(value = "students/{surname}", method = RequestMethod.GET) 
    public String showStudentBySurname(@PathVariable String surname, Model model) { 
     model.addAttribute("search", studentService.listStudentsBySurname(surname)); 
     return "students"; 
    } 
} 

私は私のローカルデータベースでの単純なCRUD操作を行うことができました。 findAllの方法で学生リストを閲覧しましたが、今はボタン付きのテキストフィールドにしたいのですが、次に何がわからないのですか?

答えて

0

あなたはそれを変更する必要があります。

@RequestMapping(value = "students/{surname}", method = RequestMethod.GET) 
    public String showStudentBySurname(@PathVariable String surname, Model model) { 
     model.addAttribute("search", studentService.listStudentsBySurname(surname)); 
     return "students"; 
    } 

へ:

@RequestMapping(value = "students", method = RequestMethod.GET) 
public String showStudentBySurname(@RequestParam (value = "surname", required = false) String surname, Model model) { 
    model.addAttribute("search", studentService.listStudentsBySurname(surname)); 
    return "students"; 
} 

フォームからパラメータを送信するので。/ [(アプリを起動)する前に]私が入れたときに、後に[[1] :あなたは(たとえば<a href="/students/surname">Surname </a>用)リンク

+0

で感謝を検索結果を得るが、それはまだ動作していないかどう@PathVariableが使用されていますデータベースにはそのような姓があります。] [2]:https:// i。[2]:https:// i。[2]:https://i.stack.imgur.com/V8qyK.jpg stack.imgur.com/jMliS.jpg – user7337867

+0

@RequestParamでvalue = "search"にvalue = "surname"を変更してください –

+0

これは大丈夫です、ありがとう! :) – user7337867

関連する問題