KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > web > AbstractClinicForm


1 package org.springframework.samples.petclinic.web;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8
9 import org.springframework.beans.propertyeditors.CustomDateEditor;
10 import org.springframework.samples.petclinic.Clinic;
11 import org.springframework.validation.BindException;
12 import org.springframework.web.bind.ServletRequestDataBinder;
13 import org.springframework.web.servlet.ModelAndView;
14 import org.springframework.web.servlet.mvc.SimpleFormController;
15
16 /**
17  * JavaBean abstract base class for petclinic-aware form controllers.
18  * Provides convenience methods for subclasses.
19  *
20  * @author Ken Krebs
21  */

22 public abstract class AbstractClinicForm extends SimpleFormController {
23
24     private Clinic clinic;
25
26     public void setClinic(Clinic clinic) {
27         this.clinic = clinic;
28     }
29
30     protected Clinic getClinic() {
31         return clinic;
32     }
33
34     public void afterPropertiesSet() {
35         if (this.clinic == null) {
36             throw new IllegalArgumentException JavaDoc("'clinic' is required");
37         }
38     }
39
40     /**
41      * Set up a custom property editor for the application's date format.
42      */

43     protected void initBinder(HttpServletRequest JavaDoc request, ServletRequestDataBinder binder) {
44         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
45         dateFormat.setLenient(false);
46         binder.registerCustomEditor(Date JavaDoc.class, new CustomDateEditor(dateFormat, false));
47     }
48
49     /**
50      * Method disallows duplicate form submission.
51      * Typically used to prevent duplicate insertion of entities
52      * into the datastore. Shows a new form with an error message.
53      */

54     protected ModelAndView disallowDuplicateFormSubmission(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
55             throws Exception JavaDoc {
56
57         BindException errors = getErrorsForNewForm(request);
58         errors.reject("duplicateFormSubmission", "Duplicate form submission");
59         return showForm(request, response, errors);
60     }
61
62 }
63
Popular Tags