2016-12-24 7 views
0

我想創建一個簡單的按鈕,通過CrudRepository的方法進行搜索,並在網站上顯示記錄。 我是個初學者,所以它可能看起來太瑣碎給你,但我問的意見仍然:)在Spring MVC,CrudRepository,Thymeleaf中搜索(通過文本字段和按鈕)

StudentRepository.java

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"; 
} 

因爲你從形式發送參數。如果你想通過鏈接(例如<a href="/students/surname">Surname </a>

+0

由於獲得搜索結果@PathVariable被使用,但它仍然不工作:/ [之前(啓動應用程序)] [1] [之後,當我把姓氏,但在數據庫中有一個這樣的姓氏] [2] [1]:https://i.stack.imgur.com/V8qyK.jpg [2]:https:// i。 stack.imgur.com/jMliS.jpg – user7337867

+0

只需在@RequestParam中將value =「surname」更改爲value =「search」 –

+0

現在沒關係,謝謝! :) – user7337867