KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.springframework.samples.petclinic.web;
2
3 import java.util.Collection JavaDoc;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7
8 import org.springframework.samples.petclinic.Owner;
9 import org.springframework.validation.BindException;
10 import org.springframework.web.servlet.ModelAndView;
11
12 /**
13  * JavaBean Form controller that is used to search for <code>Owner</code>s by last name.
14  *
15  * @author Ken Krebs
16  */

17 public class FindOwnersForm extends AbstractClinicForm {
18
19     private String JavaDoc selectView;
20
21     /** Creates a new instance of FindOwnersForm */
22     public FindOwnersForm() {
23         // OK to start with a blank command object
24
setCommandClass(Owner.class);
25     }
26
27     /**
28      * Set the name of the view that should be used for selection display.
29      */

30     public void setSelectView(String JavaDoc selectView) {
31         this.selectView = selectView;
32     }
33
34     protected void initApplicationContext() {
35         super.initApplicationContext();
36         if (this.selectView == null) {
37             throw new IllegalArgumentException JavaDoc("selectView isn't set");
38         }
39     }
40
41     /**
42      * Method used to search for owners renders View depending on how many are found.
43      */

44     protected ModelAndView onSubmit(
45             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
46             throws Exception JavaDoc {
47
48         Owner owner = (Owner) command;
49
50         // find owners by last name
51
Collection JavaDoc results = getClinic().findOwners(owner.getLastName());
52
53         if (results.size() < 1) {
54             // no owners found
55
errors.rejectValue("lastName", "notFound", null, "not found");
56             return showForm(request, response, errors);
57         }
58
59         if (results.size() > 1) {
60             // multiple owners found
61
return new ModelAndView(this.selectView, "selections", results);
62         }
63
64         // 1 owner found
65
owner = (Owner) results.iterator().next();
66         return new ModelAndView(getSuccessView(), "ownerId", owner.getId());
67     }
68
69 }
70
Popular Tags