AbstractCommandController는 요청 파라미터값을 커맨드(Command) 클래스의 필드값과 자동으로 바인딩할 때 사용한다.
파라미터와 커맨드 클래스의 데이터 바인딩은 일반적으로 알려진 JavaBeans 프로퍼티 표시법을 따른다.
AbstractCommandController을 상속받는 구현 Controller에서는 추상메소드 handle()을 구현하면 된다.
protected abstract ModelAndView handle(
HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception;
AbstractController를 이용해 만든다면, 파라미터의 값을 꺼내고 값을 객체에 담는 코드를 직접 작성해야 한다.
package com.easycompany.controller.hierarchy;
...
public class EmployeeListController extends AbstractController{
....
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//request 객체의 파라미터값을 꺼내서
String searchEid = request.getParameter("searchEid"); //사원번호
String searchDid = request.getParameter("searchDid"); //부서번호
String searchName = request.getParameter("searchName"); //사원이름
//객체에 저장한다.
SearchCriteria searchCriteria = new SearchCriteria();
searchCriteria.setSearchEid(searchEid);
searchCriteria.setSearchDid(searchDid);
searchCriteria.setSearchName(searchName);
List<Employee> employeelist = employeeService.getAllEmployees(searchCriteria);
ModelAndView modelview = new ModelAndView();
modelview.addObject("employeelist", employeelist);
modelview.addObject("searchCriteria", searchCriteria);
modelview.setViewName("employeelist");
return modelview;
}
}
package com.easycompany.controller.hierarchy;
...
public class EmployeeListController extends AbstractCommandController{
public EmployeeListController(){
//Command 객체에 대한 선언. 빈 설정 파일에 Command 객체에 대한 선언이 있다면 이 코드는 필요없다.
setCommandClass(SearchCriteria.class);
setCommandName("searchCriteria");
}
....
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
//이미 파라미터와 Command 객체의 바인딩이 되어 있다.
SearchCriteria searchCriteria = (SearchCriteria)command;
List<Employee> employeelist = employeeService.getAllEmployees(searchCriteria);
ModelAndView modelview = new ModelAndView();
modelview.addObject("employeelist", employeelist);
modelview.addObject("searchCriteria", searchCriteria);
modelview.setViewName("employeelist");
return modelview;
}
}
<bean id="employeeListController" class="com.easycompany.controller.hierarchy.EmployeeListController"
p:employeeService-ref="employeeService"
p:commandName="searchCriteria"
p:commandClass="com.easycompany.domain.SearchCriteria"/>