Alex Collins is a enterprise Java developer and currently works in the UK. He has worked in the insurance, publishing, telecoms and supply chain. In his spare time he codes in Python, Clojure and Scala (but not at the same time) and enjoys a multitude of sports. His catchphrase is K.I.S.S! Alex has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

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

08.06.2010
| 6255 views |
  • submit to reddit

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"

 

 

Published at DZone with permission of its author, Alex Collins.

(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

I'm sorry but... Where are Maven and Mercurial?

Tim Chen replied on Fri, 2010/08/06 - 10:16am in response to: Charlie Mordant

And where is 'Modern-day' web development? The clunkiness of pure java web development is anything but modern-day. Frameworks like scalatra, lift, grails (only naming the ones in the java world) that enable semantic web programming (especially when used with things like scaml) try to address this and would be more 'modern-day'.

David Karr replied on Fri, 2010/08/06 - 10:31am

You're referring to the "RequestMethod" annotation, which doesn't exist. The RequestMethod class is an enum which defines the various HTTP method variations, like "GET", "PUT", "DELETE", etcetera. An instance of RequestMethod is set on the "method" property of the "@RequestMapping" annotation.

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

A very minor correction... The IndexController.java file in the Part 1 article was supposed to be placed in 'src/main/java/com/mycompany/app/view/'. In this article, right at the top, when you do 'hg cp', its in 'src/main/java/com/mycompany/app/', along with RegisterController.java. Kinda hard to spot unless the reader actually tries out the code.

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.