July 2011 Archives

Spring 3 is great at automatically resolving standard arguments into a controller request method.  For example, a primitive Spring controller might look like this ...

@Controller
@RequestMapping(value="/somepath")
public class MyController {

@RequestMapping(method={RequestMethod.GET, RequestMethod.HEAD}) public ModelAndView someMethod(final HttpServletRequest request, final Principal principal) {
// Extract some special object needed to process the request from
// the session -- this object is bound to the session elsewhere on
// a successful authentication.
final MyObject obj = (MyObject)request.getSession().getAttribute("myobjkey");
// Do actual work.
/* ... */
return new ModelAndView("someview");
}

}

In this case, Spring knows the HttpServletRequest argument represents the incoming Servlet request, and the Principal argument is the object representing the authenticated user (in the event that you're using Spring Security to manage authentication in your web-application).  On method invocation, Spring automatically resolves these arguments for you.  Neat!

However, the repetition becomes obvious where in every controller, you need to fetch the same MyObject from the session, over and over again.  Instead of repeating that line of code in every method of every controller that needs access to MyObject, what if you could tell Spring how to resolve MyObject automatically on invocation?

Twitter (@markkolich)

Translate

About this Archive

This page is an archive of entries from July 2011 listed from newest to oldest.

January 2011 is the previous archive.

December 2011 is the next archive.

Find recent content on the main index or look in the archives to find all content.