Thursday, September 17, 2009

How to determine the type of Request with Spring MVC's @InitBinder

If you want to determine the request method of a request (i.e."POST", "GET" etc) in Spring MVC, using a SimpleFormController you could just call isFormSubmission(). When using annotations, the @RequestMapping annotation gives you that functionality for free in the annotation parameters.

If you want to achieve the same thing on a method annotated with @InitBinder, the same does not apply. However, you can simply do this:

@InitBinder
public void initBinder(HttpServletRequest request) {
if ("POST".equals(request.getMethod()){
//Do something
}
}


Why would you want to know the type of request in an @InitBinder method? Stay tuned to find out one pretty cool application.

0 comments: