Monday, August 9, 2010

Adding Spring Web MVC part 2

Now I'll show you a couple of examples how it looks like a Controller & a JSP using Spring MVC.

From my personal perspective Defining a controller gives you a lot of power, because you can define a method to handle RequestMethod.GET, just to show a Form, and another method to handle the RequestMethod.POST to handle the "submit", cool isn't it?

By adding @Controller you already told Spring, "hey this dude is going to be a Controller".

@RequestMapping(value="/configuration/*") means: "If my URL is WebContext/configuration/ this Controller is going to handle the Request"

Then on each method I can define my path below /configuration/ @RequestMapping(value = "showConsumersConfiguration.do"), something like this WebContext/configuration/showConsumersConfiguration.do

I almost forgot @Autowired which is for inject implementation of that Bean


If you want to handle an object like a form (bind an object to a form), check this out

Important, include these tag libraries!

By using this attribute modelAttribute="UserVO" of form:form tag, I'm binding this line of my controller: modelMap.put("UserVO", userVO); with my form

If you see carefully, each attribute of my VO, has a reference on my JSP, at least as hidden field, but all of them are there

Now my form has been binded to my VO @ModelAttribute UserVO paramUserVO

Now you're ready to play around with this thing!!!

Always go to the official documentation to get more details!!!

No comments:

Post a Comment