Alex
Alex

Reputation: 2125

Spring Controller info

Do this two methods of a @Controller the same thing?In what they differ? This is a Spring mvc example with a form. The first method adds an object to the model or not ??Thanks

@Controller
public class HomeController{

@RequestMapping(method = RequestMethod.GET)
public Member form() { 
  return new Member();
}


@RequestMapping(method = RequestMethod.GET)
public void form() { 
 model.addAttribute(new Member());

}
}

Upvotes: 0

Views: 29

Answers (1)

rocky
rocky

Reputation: 5004

your code wont compile, but if you change it to for example:

@RequestMapping(method = RequestMethod.GET)
public ModelAndView form(ModelAndView model) { 
model.addObject("member", new Member());
model.setViewName("view");
return model

}

it will try to return view.html ( that depends how do you configure ViewResolver ), and Member object will be available under name "member"

for template library like Freemarker - so you can print something from it to user.

First method can be used for example for REST api, so you return member to anyone who calls your HomeController.

Upvotes: 1

Related Questions