Modern-day web development with Maven, Mercurial and Spring MVC - Part 2
A couple of weeks ago, I posted an article demonstrating how easy web developement in Java is these days. This article extends the previous and takes the Spring MVC annotated approach to a more real-world level.
The Plunge
The first thing we'll do is create a new controller for us to play with. Let's get hg to do it for us:
$ hg cp src/main/java/com/mycompany/app/view/IndexController.java src/main/java/com/mycompany/app/view/RegisterController.java
Now that's done, let's edit the content of RegisterController and run through some basics of the Spring MVC annotation approach.
@Controller
@RequestMapping("/register.html")
public class RegisterController {
@RequestMapping(method=RequestMethod.GET)
public String indexHandler() {
return "register";
}
@RequestMapping(method=RequestMethod.PUT)
public String submitHandler(
@RequestParam("username") String username,
@RequestParam("pass") String pass,
@RequestParam("passConf") String passConf) {
// validate:
// - username is not null and length >= 3
// - pass is not null and > 5
// - passConf equals pass
}
}
So here we have our 'stub' controller. The idea is that we'll have a register.jsp that holds a form for three fields: a username, password and password confirmation. Very simple, very easy.
RequestMethod
Perhaps the simplest of the three new concepts demonstrated above, we use RequestMethod for mapping requests via their...method! The above class is a good example of where this comes in handy; it allows for one RequestMapping and no need for fine-grained or individual URIs. Unlike RequestMappingand RequestParam, RequesMethod is just an Enum defining GET and POST.
@RequestMapping
The RequestMapping annotation can be used at both the type and method level. Set at the type level, the class is then mapped to one URL or wildcard. This is useful when you have say traditional Spring MVC controllers that extend for example SimpleFormController, allowing you to plug-and-play this additional style of controller declaration without having too much of a rewrite.
At the method level, you can be more fine-grained with your request URI choices. The 'submitHandler' method above is one example of a method that would 'inherit' its mapping from the type-level RequestMapping annotation. Both methods in the class are doing this, except each is configured for a different request method - GET or POST.
@RequestParam
We use the RequestParam annotation for mapping request parameters - GET or POST - from the request to method parameters. It's that simple. You pass an identifier as a String to the annotation and optionally "required = false" and the value of that parameter will be set to the request equivalent should it be present.
Here's an example of an additional parameter that is non-mandatory.
@RequestMapping(method=RequestMethod.PUT)
public String submitHandler(
@RequestParam("username") String username,
@RequestParam("pass") String pass,
@RequestParam("passConf") String passConf,
@RequestParam("email", required = false) String email) {
}
I personally think this is a much-untouched approach to web development in Java. You don't see it that often around the web (perhaps I'm looking in the wrong places) yet it's elegance and simplicity means your productivity goes through the roof. Compare this to a Struts application - could you have written less?
The next installment will be dedicated to the ModelAttribute and SessionAttributes which significantly augment applications yet still give us the simplicity you've seen already.
Oh, one last thing - don't forget to commit your changes with hg:
$ hg com -m "Integrating RequestParams and RequestMethods in RegisterController"
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Charlie Mordant replied on Fri, 2010/08/06 - 6:49am
Tim Chen replied on Fri, 2010/08/06 - 10:16am
in response to:
Charlie Mordant
David Karr replied on Fri, 2010/08/06 - 10:31am
Alex Collins replied on Fri, 2010/08/06 - 4:16pm
in response to:
Charlie Mordant
The title was purely taken from the previous installment and kept for consistency.
Thanks for reading!
Alex Collins replied on Fri, 2010/08/06 - 4:18pm
in response to:
David Karr
Of course this is - thanks for letting me know. Bit of a slip when initially 'fleshing out' the article.
Thanks for reading!
Joel Realubit replied on Sat, 2010/08/07 - 4:32pm
Looking forward to the next installment!
Alex Collins replied on Mon, 2010/08/09 - 5:29am
in response to:
Joel Realubit
Very helpful, thanks a lot for letting me know :)
Thanks for reading!
Darren Martin replied on Mon, 2012/11/05 - 10:24am
Many changes has been done for web development as of these days, so for sure, some developer are very hands on working with Java, easiest than this old ideas.
Cheers,
Darren of OneIms Ltd.