views:

405

answers:

2

Is there a tutorial that goes along with the PetClinic application? I have been trying to find one, but google is not helping me today. Specifically, I dont understand things like:

@Autowired - what does that even mean?

@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
    Pet pet = this.clinic.loadPet(petId);
    model.addAttribute("pet", pet);
    return "petForm";
}

How can a request return just a string? Shouldnt it need to return some sort of ModelAndView? Or does the application somehow redirect to whatever is returned?

A lot of confusing concepts - if there is a tutorial, or video (like spring-security has) that would be very helpful. Thanks.

+1  A: 

Autowired is Dependency injection. It creates beans for you and sets them.

In this example the controller is returning a String that is the name of the view. It's basically the same thing as

return new ModelAndView("petForm"); 

It could map to something else or it could be as simple as returning petForm.jsp. Depends on the View Resolver.

BrennaSoft
So, does the @Autowired call setters a bean based on the passed in POST/GET variables?
wuntee
also - what is public String setupForm(@RequestParam("petId") int petId, ModelMap model) how does ModelMap get set? The method will be called on any GET, correct?
wuntee
one last thing - when you simply return the String of a view, and the resolver uses a Freemarker template, are there any objects set in the ModelAndView object?
wuntee
@Autowired is used to set dependencies on beans, like setting service objects or daos, has nothing to do with POST/GET variables, mapping those variables is done with the @RequestParam annotation.If you add a ModelMap parameter in the parameter list, Spring just creates it for you, to populate with data for the view.A ModelAndView object is just an encapsulation of a Model and a View. So the name of the ModelAndView is the name of the view, and any data you want available to the view must be set on the ModelAndView by addObject.
BrennaSoft
Then how does @Autowired grab the depndancy? From the application-context.xml?
wuntee
Is the ModelMap just empty? What is the benefit of creating it in the method institution variables vs just doing "new ModelMap()" as the first line?
wuntee
in that sample code above, it adds information to the model, yet it only returns a string. Does the modelmap actually get passed to the view? and why not just pass back a ModelAndView vs a String?
wuntee
@Autowired either grabs dependences from applicationContext or you can annotate the beans with @Repository, @Controller, @Service, or @Component and not need to define them in applicationContext.xml
BrennaSoft
Yes the view will have access to the modelMap. From my experience I just always return the ModelAndView object. As with new spring releases they come up with new ways to do things but still leave in the older ways, so there are 100 ways to do everything.
BrennaSoft
A: 

It is returning the exact name of the view, so you could expect this to redirect you to the view located at /WEB-INF/jsp/petForm.jsp which will have access to the pet model

Flash84x