package
com.apress.springrecipes.court.web;
...
import org.springframework.beans.factory.annotation.Autowired;
...
@Controller
@RequestMapping("/reservationForm.htm")
@SessionAttributes("reservation")
public class ReservationFormController{
private
ReservationService reservationService;
private
ReservationValicator validator;
@Autowired
public
ReservationFormController(ReservationService reservationService,
ReservationValidator validator){
this.reservationService = reservationService;
this.validator = validator;
}
@InitBinder
public
void initBinder(WebDataBinder binder){
SimpleDateFormat dateFormat = new
SimpleDateFormat("yyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,new
CustomDateEditor(
dateFormat,true));
binder.registerCustomEditor(SportType.class,new
SportTypeEditor(
reservationService));
}
@ModelAttribute("sportTypes")
public
List<SportType> populateSportTypes(){
return
reservationService.getAllSportTypes();
}
@RequestMapping(method=RequestMethod.GET)
public
String setupForm(@RequestParam(required=false,value="username")
String username,ModelMap model){
Reservation reservation = new
Reservation();
reservation.setPlayer(new
Player(username,null));
model.addAttribute("reservation",reservation);
return
"reservationForm";
}
@RequestMapping(method=RequestMethod.POST)
public
String processSubmit(@ModelAttribute("reservation")Reservation reservation,
BindingResult result,SessionStatus status){
validator.validate(reservation,result);
if(result.hasErrors()){
return
"reservationForm";
}else{
reservationService.make(reservation);
status.setComplete();
return
"redirect:reservationSuccess.htm";
}
}
}