2016-07-01 9 views
1

스프링 부트 & 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> 

답변

1

하십시오 HTTP의 POST에 대한 입력 매개 변수 객체 $ {학생} 메서드를 구현해야합니다. 또한 모델에 투입되는 입력 필드의 데이터 양식을 제출 '포스트'

<form th:object="${student}" th:action="@{/search}" action="#" method="post"> 

에 양식의 방법을 설정해야 전송한다

@RequestMapping(method=RequestMethod.POST, value="/search") 
public ModelAndView doSearch(Student student){ 
    // do your conditional logic in here to check if form parameters were populated or not 
    // then do something about getting results from the repository 
    List<String> students = repository.find....; 
    // return a model and a view (just as an example) 
    ModelAndView mv = new ModelAndView(); 
    mv.addObject(students); 
    mv.setViewName("/results"); 
    return mv; 
} 

: 뭔가 것처럼해야한다 HTTP POST를 통해 다음을 참조하십시오. http://www.w3schools.com/tags/att_form_method.asp

또는 URL 매개 변수의 양식 필드를 구문 분석하는 두 번째 다른 GET 요청 매핑을 추가 할 수 있습니다.

@RequestMapping(value = {"/search"}, method = RequestMethod.GET) 
public String doSearch(@PathVariable String firstName, @PathVariable String lastName, @PathVariable String city) { 
    // Add your conditional logic to search JPA repository based on @PathVariable values delivered from form submission using HTTP GET 

    List<String> students = repository.find....; 
    ModelAndView mv = new ModelAndView(); 
    mv.addObject(students); 
    mv.setViewName("/results"); 
    return mv; 
} 

을하지만 한계를 인식 할 수 및 폼 데이터를 전송하기 위해 '얻을'= 형태의 방법을 사용하여 보안에 미치는 영향 : '수', 다음과 같이 다른 GET 요청 매핑을 추가로 양식 방법을 떠난다.

+0

예제를 사용하여 findAll을 사용했지만 오류가 발생했습니다. ------------ 예기치 않은 오류가 발생했습니다 (type = Method Not Allowed, status = 405). 요청 방법 'GET'이 지원되지 않습니다. ---------------- method = RequestMethod.POST를 제거하면 작동하지만 기록이 표시되지 않습니다. – Muhammad

+0

위의 편집이 도움이되어야한다고 생각합니다. :) –

+1

나는 그뿐 아니라 행운을 시험해 보았습니다. ( – Muhammad

관련 문제