KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > demo > icare > web > CarFormController


1 package org.objectweb.petals.demo.icare.web;
2
3 import java.beans.PropertyEditorSupport JavaDoc;
4 import java.text.NumberFormat JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
25  * The controller for editing a car
26  *
27  * @author Cristian Spiescu
28  * @since 17.01.2007
29  */

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 JavaDoc
49     protected void initBinder(HttpServletRequest JavaDoc request, ServletRequestDataBinder binder) throws Exception JavaDoc {
50         //binder.registerCustomEditor(null, "category", new PropertyEditorSupport());
51
// convert java.lang.Long
52
NumberFormat JavaDoc nf = NumberFormat.getNumberInstance();
53         binder.registerCustomEditor(Long JavaDoc.class, null,
54                 new CustomNumberEditor(Long JavaDoc.class, nf, true));
55         
56         binder.registerCustomEditor(null, "category", new PropertyEditorSupport JavaDoc() {
57
58             @Override JavaDoc
59             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
60                 Integer JavaDoc id = new Integer JavaDoc(text);
61                 Category cat = carService.findCategoryById(id);
62                 setValue(cat);
63             }
64             
65         });
66
67         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("dd/MM/yy HH:mm");
68         dateFormat.setLenient(false);
69         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(dateFormat, true));
70         
71         super.initBinder(request, binder);
72     }
73
74     public ModelAndView processFormSubmission(HttpServletRequest JavaDoc request,
75             HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
76             throws Exception JavaDoc {
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 JavaDoc("unchecked")
85     protected Map JavaDoc referenceData(HttpServletRequest JavaDoc request) throws Exception JavaDoc {
86         Map JavaDoc refData = new HashMap JavaDoc();
87         refData.put("categories", carService.getAllCategories());
88         return refData;
89     }
90     
91     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request)
92             throws Exception JavaDoc {
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 JavaDoc request,
104             HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
105             throws Exception JavaDoc {
106
107         Car car = (Car) command;
108         if (request.getParameter("delete") != null) {
109             //carService.deleteCar(car.getId());
110
} else {
111             carService.storeCar(car);
112         }
113
114         return new ModelAndView(getSuccessView());
115     }
116     
117     
118         
119 }
120
Popular Tags