Modern-day web development with Maven, Mercurial and Spring MVC - Part 2

DZone 's Guide to

Modern-day web development with Maven, Mercurial and Spring MVC - Part 2

· Java Zone ·
Free Resource

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.

public class RegisterController {

public String indexHandler() {
return "register";

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.


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.


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. 


 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.

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"




Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}