스프링 부트 & thymeleaf를 사용하여 데이터베이스에서 학생을 검색하는 페이지를 만들었습니다. 내 SearchStudent.html
페이지에는 검색 매개 변수 (이름, 성, 도시)로 3 개의 필드가 있습니다. 내 요구 사항은 매개 변수를 입력하지 않더라도 (모두 검색) 또는 매개 변수를 기반으로 검색을 수행해야한다는 것입니다. '모두 검색'조건이 작동하지만 검색 조건의 일부 또는 모든 매개 변수를 전달할 때 컨트롤러가 작동하도록 변경하는 방법을 잘 모릅니다.springboot에서 컨트롤러에 매개 변수 값을 전달하는 방법
SearchController
@Controller
@ComponentScan
public class SearchController {
@Autowired
protected StudentRepository repository;
@RequestMapping(value = {"/","/search"}, method = RequestMethod.GET)
public String search(Model model) {
model.addAttribute("student", new Student());
model.addAttribute("allStudents", (ArrayList<Student>)repository.findAll());
return "SearchStudent";
}
SearchStudent.html 양식은 일에 양식 필드 결합
<div class="panel-body">
<form th:object="${student}" th:action="@{/search}" action="#" method="get">
<input type="text" th:field="*{firstName}" class="form-control" placeholder="First Name" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="text" th:field="*{lastName}" class="form-control" placeholder="Last Name" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="text" th:field="*{city}" class="form-control" placeholder="City" />
<div style="clear: both; display: block; height: 10px;"></div>
<input type="submit" class="btn btn-danger pull-right" value="Search">
<input type="submit" class="btn btn-success pull-right" value="Clear">
</form>
</div>
예제를 사용하여 findAll을 사용했지만 오류가 발생했습니다. ------------ 예기치 않은 오류가 발생했습니다 (type = Method Not Allowed, status = 405). 요청 방법 'GET'이 지원되지 않습니다. ---------------- method = RequestMethod.POST를 제거하면 작동하지만 기록이 표시되지 않습니다. – Muhammad
위의 편집이 도움이되어야한다고 생각합니다. :) –
나는 그뿐 아니라 행운을 시험해 보았습니다. ( – Muhammad