View Javadoc

1   
2   package org.springframework.samples.petclinic.web;
3   
4   import org.springframework.beans.factory.annotation.Autowired;
5   import org.springframework.samples.petclinic.Clinic;
6   import org.springframework.samples.petclinic.Vets;
7   import org.springframework.stereotype.Controller;
8   import org.springframework.ui.ModelMap;
9   import org.springframework.web.bind.annotation.PathVariable;
10  import org.springframework.web.bind.annotation.RequestMapping;
11  import org.springframework.web.bind.annotation.RequestMethod;
12  import org.springframework.web.servlet.ModelAndView;
13  
14  /**
15   * Annotation-driven <em>MultiActionController</em> that handles all non-form
16   * URL's.
17   *
18   * @author Juergen Hoeller
19   * @author Mark Fisher
20   * @author Ken Krebs
21   * @author Arjen Poutsma
22   */
23  @Controller
24  public class ClinicController {
25  
26  	private final Clinic clinic;
27  
28  
29  	@Autowired
30  	public ClinicController(Clinic clinic) {
31  		this.clinic = clinic;
32  	}
33  
34  	/**
35  	 * Displays the the welcome view.
36  	 * <p>
37  	 * Note that this handler relies on the RequestToViewNameTranslator to
38  	 * determine the logical view name based on the request URL: "/welcome.do"
39  	 * -&gt; "welcome".
40  	 */
41  	@RequestMapping("/")
42  	public String welcomeHandler() {
43  		return "welcome";
44  	}
45  
46  	/**
47  	 * Displays all vets.
48  	 *
49  	 * <p>Note that this handler returns a plain {@link ModelMap} object instead of
50  	 * a ModelAndView, thus leveraging convention-based model attribute names.
51  	 * It relies on the RequestToViewNameTranslator to determine the logical
52  	 * view name based on the request URL: "/vets.do" -&gt; "vets".
53  	 *
54  	 * @return a ModelMap with the model attributes for the view
55  	 */
56  	@RequestMapping("/vets")
57  	public ModelMap vetsHandler() {
58  		Vets vets = new Vets();
59  		vets.getVetList().addAll(this.clinic.getVets());
60  		return new ModelMap(vets);
61  	}
62  
63  	/**
64  	 * Displays the specified owner.
65  	 *
66  	 * @param ownerId the ID of the owner to display
67  	 * @return a ModelMap with the model attributes for the view
68  	 */
69  	@RequestMapping("/owners/{ownerId}")
70  	public ModelAndView ownerHandler(@PathVariable("ownerId") int ownerId) {
71  		ModelAndView mav = new ModelAndView("owners/show");
72  		mav.addObject(this.clinic.loadOwner(ownerId));
73  		return mav;
74  	}
75  
76  	/**
77  	 * Displays a list of all visits for a specified pet.
78  	 *
79  	 * @param petId the ID of the pet whose visits to display
80  	 * @return a ModelMap with the model attributes for the view
81  	 */
82  	@RequestMapping(value="/owners/*/pets/{petId}/visits", method=RequestMethod.GET)
83  	public ModelAndView visitsHandler(@PathVariable int petId) {
84  		ModelAndView mav = new ModelAndView("visits");
85  		mav.addObject("visits", this.clinic.loadPet(petId).getVisits());
86  		return mav;
87  	}
88  
89  }