1 package org.objectweb.petals.demo.icare.web; 2 3 import java.beans.PropertyEditorSupport ; 4 import java.text.NumberFormat ; 5 import java.text.SimpleDateFormat ; 6 import java.util.Date ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 13 import org.objectweb.petals.demo.icare.model.Car; 14 import org.objectweb.petals.demo.icare.model.Category; 15 import org.objectweb.petals.demo.icare.service.CarService; 16 import org.springframework.beans.propertyeditors.CustomDateEditor; 17 import org.springframework.beans.propertyeditors.CustomNumberEditor; 18 import org.springframework.validation.BindException; 19 import org.springframework.web.bind.RequestUtils; 20 import org.springframework.web.bind.ServletRequestDataBinder; 21 import org.springframework.web.servlet.ModelAndView; 22 import org.springframework.web.servlet.mvc.SimpleFormController; 23 24 30 public class CarFormController extends SimpleFormController { 31 32 private CarService carService; 33 34 public CarService getCarService() { 35 return carService; 36 } 37 38 public void setCarService(CarService carService) { 39 this.carService = carService; 40 } 41 42 public CarFormController() { 43 setSessionForm(true); 44 setBindOnNewForm(true); 45 setValidateOnBinding(true); 46 } 47 48 @Override 49 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { 50 NumberFormat nf = NumberFormat.getNumberInstance(); 53 binder.registerCustomEditor(Long .class, null, 54 new CustomNumberEditor(Long .class, nf, true)); 55 56 binder.registerCustomEditor(null, "category", new PropertyEditorSupport () { 57 58 @Override 59 public void setAsText(String text) throws IllegalArgumentException { 60 Integer id = new Integer (text); 61 Category cat = carService.findCategoryById(id); 62 setValue(cat); 63 } 64 65 }); 66 67 SimpleDateFormat dateFormat = new SimpleDateFormat ("dd/MM/yy HH:mm"); 68 dateFormat.setLenient(false); 69 binder.registerCustomEditor(Date .class, new CustomDateEditor(dateFormat, true)); 70 71 super.initBinder(request, binder); 72 } 73 74 public ModelAndView processFormSubmission(HttpServletRequest request, 75 HttpServletResponse response, Object command, BindException errors) 76 throws Exception { 77 if (request.getParameter("cancel") != null) { 78 return new ModelAndView(getSuccessView()); 79 } 80 81 return super.processFormSubmission(request, response, command, errors); 82 } 83 84 @SuppressWarnings ("unchecked") 85 protected Map referenceData(HttpServletRequest request) throws Exception { 86 Map refData = new HashMap (); 87 refData.put("categories", carService.getAllCategories()); 88 return refData; 89 } 90 91 protected Object formBackingObject(HttpServletRequest request) 92 throws Exception { 93 long carId = RequestUtils.getIntParameter(request, "id", -1); 94 Car car = null; 95 if (carId == -1) { 96 car = new Car(); 97 } else { 98 car = carService.findCarById(carId); 99 } 100 return car; 101 } 102 103 protected ModelAndView onSubmit(HttpServletRequest request, 104 HttpServletResponse response, Object command, BindException errors) 105 throws Exception { 106 107 Car car = (Car) command; 108 if (request.getParameter("delete") != null) { 109 } else { 111 carService.storeCar(car); 112 } 113 114 return new ModelAndView(getSuccessView()); 115 } 116 117 118 119 } 120 | Popular Tags |