问题
I want create a simple button to search, via methods of CrudRepository, and show records on the website. I'm a beginner, so it may seem too trivial to you, but I'm asking for advice nevertheless :)
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";
}
}
I managed to do simple CRUD with my local database. I have done viewing the list of students by method findAll directly on the website, but now I want do it for text field with button, but I don't know what next.
回答1:
You need change it:
@RequestMapping(value = "students/{surname}", method = RequestMethod.GET)
public String showStudentBySurname(@PathVariable String surname, Model model) {
model.addAttribute("search", studentService.listStudentsBySurname(surname));
return "students";
}
to:
@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";
}
Because you send parameters from the form. @PathVariable
is used if you want get search result by link(for example <a href="/students/surname">Surname </a>
)
来源:https://stackoverflow.com/questions/41314724/search-via-text-field-and-button-in-spring-mvc-crudrepository-thymeleaf